raptor1 commited on
Commit
3a6ab29
·
verified ·
1 Parent(s): 4a90d86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -49
app.py CHANGED
@@ -1,64 +1,95 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
 
 
4
  """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
 
 
 
 
 
 
 
 
 
 
6
  """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
 
 
 
25
 
26
- messages.append({"role": "user", "content": message})
 
27
 
28
- response = ""
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
41
 
 
42
 
 
43
  """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- if __name__ == "__main__":
64
- demo.launch()
 
1
+ #importing libraries
2
+ import gdown
3
+ import pandas as pd
4
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
5
+
6
+
7
+ """The model and tokenizer must be properly initialized before we use the get_answer function otherwise it the UI will not show answers generated by the model because model has not generated any output yet."""
8
+
9
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
10
+
11
+ # Initialize the tokenizer and model
12
+ tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
13
+ model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base").to("cpu") ##using "cpu" instead of "gpu" (GPU) as deploying on HF free tier of CPU processor
14
+
15
+
16
+ """Loading the FAQs csv from Google drive into a Python dictionary"""
17
+
18
+ # Download the CSV file using gdown
19
+ file_id = "1O4CWfDo9h7MDK5KH5fktJeSTleoew6OY" # file ID from google drive
20
+ gdown.download(f'https://drive.google.com/uc?id={file_id}', 'faqs.csv', quiet=False)
21
+
22
+
23
+ """
24
+ Tried downloading directly from drive, did not work properly. Could have also used upload feature in Colab.
25
+ import pandas as pd
26
 
27
+ # Load FAQs from Google Drive.
28
+ data_url = "https://drive.google.com/uc?export=download&id=1O4CWfDo9h7MDK5KH5fktJeSTleoew6OY"
29
  """
30
+
31
+
32
+ #creating dataframe
33
+ df = pd.read_csv(data_url)
34
+
35
+ #Converting df to dictionary for easy retrival of Que/Ans instead of using index of dataframe.
36
+ faqs = df.to_dict(orient='records') # Convert to dictionary
37
+ print("Loaded FAQs:", faqs)
38
+
39
+ """Chat bot logic :
40
+ Retrieves an answer from FAQs dataset based on user input. using Google Flan-T5 model to refine the answer. Flan-T5 is a powerful transformer-based language model.
41
  """
 
42
 
43
+ def get_answer(question):
44
+ # Finding the closest FAQ question using User input query
45
+ closest_faq = None
46
+ for faq in faqs:
47
+ if question.lower() in faq["Question"].lower():
48
+ closest_faq = faq
49
+ break
50
 
51
+ # If query does not match, return a default response
52
+ if not closest_faq:
53
+ return "Please contact support for further assistance. Thanks!"
 
 
 
 
 
 
54
 
55
+ # Using Google Flan-T5 model to refine the answer.
56
+ #Creating Prompt string for the model to process.
57
+ #{closest_faq["Answer"]} fetch only answer from FAQs as each FAQ entry contains fields "Question," "Answer," and "Category".
58
+ #By including 'Answer' from the FAQ,it guide the model to produce a response that is informed by existing information(context).
59
+ prompt = f"""
60
+ Question: {question}
61
+ Answer this question using ONLY the context below:
62
+ Context: {closest_faq["Answer"]}
63
+ """
64
 
65
+ #prompt string is tokenized using a HF tokenizer
66
+ inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
67
 
68
+ #Flan-T5 model generates an answer based on the inputs, with a maximum length of 200 tokens.
69
+ #The Flan-T5 model processes the tokenized inputs and generates an answer.
70
+ outputs = model.generate(**inputs, max_length=200)
71
 
72
+ #Generated output is decoded back into human-readable text using the tokenizer, removing any special tokens.
73
+ answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
74
 
75
+ # Include the category in the response
76
+ return f"CATEGORY :\n {closest_faq['Category']}\nANSWER :\n {answer}"
77
 
78
+ """Using closest_faq["Answer"] in the context provides relevant, structured information that enhances the quality of responses generated by the Flan-T5 model. It ensures that answers are based on verified content rather than relying solely on general knowledge, which is vital for applications like customer support.Cobining language processing to deliver precise and contextually appropriate answers.
79
 
80
+ Building simple UI using Gardio
81
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
+ import gradio as gr
84
+
85
+ # Simple chat interface
86
+ demo = gr.Interface(
87
+ fn=get_answer,
88
+ inputs=gr.Textbox(label="Ask a question"),
89
+ outputs=gr.Textbox(label="Answer"),
90
+ title="SaaS Support Chatbot",
91
+ examples=["How do I cancel my subscription?", "I forgot my password"]
92
+ )
93
 
94
+ # Launchin UI in Colab
95
+ demo.launch(share=True) # Generates a public link like