AnaviJoshi commited on
Commit
0e374b2
·
verified ·
1 Parent(s): f888f21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -80
app.py CHANGED
@@ -2,83 +2,49 @@ import gradio as gr
2
  from sentence_transformers import SentenceTransformer
3
  import torch
4
  from huggingface_hub import InferenceClient
5
- #import os
6
-
7
- #HF_TOKEN = os.environ.get("HF_TOKEN")
8
 
 
9
  with open("knowledge.txt", "r", encoding="utf-8") as file:
10
- knowledge = file.read()
11
-
12
- cleaned_text = knowledge.strip()
13
- # cleaning up the text
14
- chunks = cleaned_text.split("\n")
15
- # separating the text into one sentence chunks
16
- cleaned_chunks = []
17
- # creating a empty list called cleaned_chunks
18
-
19
- for chunk in chunks:
20
- # for every chunk in the chunks list,
21
- stripped_chunk = chunk.strip()
22
- # the chunk is getting stripped
23
- if stripped_chunk:
24
- #if the chunk is not empty then it is being appended to the cleaned chunk list.
25
- cleaned_chunks.append(stripped_chunk)
26
  model = SentenceTransformer('all-MiniLM-L6-v2')
27
  chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
28
- # encode the model, pass through my clean chunks and convert to vector embeddings (not arrays)
29
-
30
-
31
-
32
- def get_top_chunks(query): # store a function that gets the most relevant_info and make it return a variable “relevant_info” then
33
- # create my function taking query as my parameter
34
- query_embedding = model.encode(query, convert_to_tensor=True)
35
- # turning my query to a vector embedding for comparison
36
- query_embedding_normalized = query_embedding / query_embedding.norm()
37
- # normlaize my query to 1: allows for comparison of meaning
38
- chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
39
- # normalizing chunks for comparison of meaning
40
-
41
- similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
42
- # using my matmul(matrix multiplication method to compare query to chunks)
43
-
44
- top_indices = torch.topk(similarities, k=3).indices
45
- # get the indices of the chunks that are most similar to my query
46
-
47
- top_chunks = []
48
-
49
-
50
- for i in top_indices:
51
- chunk = chunks[i]
52
- # for each index number in top_indices, get back the text
53
- top_chunks.append(chunk) # adding the sentences that are associated with the top indices to the list
54
-
55
- return top_chunks
56
 
 
 
 
 
 
 
 
 
57
 
 
58
  client = InferenceClient("google/gemma-3-27b-it")
59
 
60
-
61
  def respond(message, history):
62
  knowledge_base = get_top_chunks(message)
63
- system_message = f"You are a helpful chatbot named scooby, kinda like the cartoon character but not too much like it, who knows alot about pets and their diets and loves helping pet owners, and can only answer questions about pets:{knowledge_base}"
64
- messages = [{"role": "system", "content": system_message}]
65
-
66
 
67
-
68
  if history:
69
- messages.extend(history)
70
-
71
  messages.append({"role": "user", "content": message})
 
72
  response = ""
73
- for messages in client.chat_completion(messages,max_tokens = 2500, stream = True):
74
- token = messages.choices[0].delta.content
75
  response += token
76
  yield response
77
 
 
 
 
78
 
79
-
80
-
81
- theme = gr.themes.Monochrome(primary_hue="teal",secondary_hue="orange")
82
 
83
  welcome_message = """
84
  # Welcome to ScoobyAI
@@ -92,11 +58,7 @@ topics = """
92
  - Creating recipes for pets based on dietary needs
93
  """
94
 
95
-
96
- #chatbot = gr.ChatInterface(respond, type = "messages", theme)
97
-
98
- import gradio as gr
99
-
100
  with gr.Blocks(theme=theme) as demo:
101
  gr.Image(
102
  value="scooby.png",
@@ -107,18 +69,17 @@ with gr.Blocks(theme=theme) as demo:
107
  show_download_button=False,
108
  container=False
109
  )
110
-
111
  gr.Markdown(welcome_message)
112
-
113
  with gr.Row():
114
  with gr.Column():
115
  gr.Markdown(topics)
116
-
117
  with gr.Tabs():
118
  with gr.Tab("Main Page"):
119
  with gr.Row():
120
  text_input = gr.Textbox(label="Ask Scooby a question about your pet")
121
- # image_input = gr.Image(type="pil", label="Upload an image of your pet (optional)")
122
  output = gr.Textbox(label="Scooby's Answer")
123
  submit_btn = gr.Button("Ask Scooby")
124
  submit_btn.click(fn=respond, inputs=text_input, outputs=output)
@@ -136,21 +97,9 @@ with gr.Blocks(theme=theme) as demo:
136
  clear_btn="🧹 Clear Chat"
137
  )
138
 
139
-
140
-
141
-
142
-
143
  demo.launch(debug=True)
144
 
145
 
146
 
147
 
148
 
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
 
2
  from sentence_transformers import SentenceTransformer
3
  import torch
4
  from huggingface_hub import InferenceClient
 
 
 
5
 
6
+ # Load and process knowledge base
7
  with open("knowledge.txt", "r", encoding="utf-8") as file:
8
+ knowledge = file.read()
9
+
10
+ cleaned_chunks = [chunk.strip() for chunk in knowledge.strip().split("\n") if chunk.strip()]
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  model = SentenceTransformer('all-MiniLM-L6-v2')
12
  chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ # Function to retrieve most relevant info
15
+ def get_top_chunks(query):
16
+ query_embedding = model.encode(query, convert_to_tensor=True)
17
+ query_embedding_normalized = query_embedding / query_embedding.norm()
18
+ chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
19
+ similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
20
+ top_indices = torch.topk(similarities, k=3).indices
21
+ return [cleaned_chunks[i] for i in top_indices]
22
 
23
+ # Connect to HF inference client
24
  client = InferenceClient("google/gemma-3-27b-it")
25
 
26
+ # Chatbot function
27
  def respond(message, history):
28
  knowledge_base = get_top_chunks(message)
29
+ system_message = f"You are a helpful chatbot named Scooby, kind of like the cartoon character but not too much like it. You know a lot about pets and their diets and can only answer questions about pets. Here is what you know: {knowledge_base}"
 
 
30
 
31
+ messages = [{"role": "system", "content": system_message}]
32
  if history:
33
+ messages += history
 
34
  messages.append({"role": "user", "content": message})
35
+
36
  response = ""
37
+ for delta in client.chat_completion(messages=messages, max_tokens=2500, stream=True):
38
+ token = delta.choices[0].delta.get("content", "")
39
  response += token
40
  yield response
41
 
42
+ # Recipe chatbot (simple placeholder)
43
+ def respond_recipe(message, history):
44
+ return f"Here's a custom recipe idea for your pet based on: '{message}'. 🐾"
45
 
46
+ # Theme, welcome, topics
47
+ theme = gr.themes.Monochrome(primary_hue="teal", secondary_hue="orange")
 
48
 
49
  welcome_message = """
50
  # Welcome to ScoobyAI
 
58
  - Creating recipes for pets based on dietary needs
59
  """
60
 
61
+ # Gradio UI
 
 
 
 
62
  with gr.Blocks(theme=theme) as demo:
63
  gr.Image(
64
  value="scooby.png",
 
69
  show_download_button=False,
70
  container=False
71
  )
72
+
73
  gr.Markdown(welcome_message)
74
+
75
  with gr.Row():
76
  with gr.Column():
77
  gr.Markdown(topics)
78
+
79
  with gr.Tabs():
80
  with gr.Tab("Main Page"):
81
  with gr.Row():
82
  text_input = gr.Textbox(label="Ask Scooby a question about your pet")
 
83
  output = gr.Textbox(label="Scooby's Answer")
84
  submit_btn = gr.Button("Ask Scooby")
85
  submit_btn.click(fn=respond, inputs=text_input, outputs=output)
 
97
  clear_btn="🧹 Clear Chat"
98
  )
99
 
 
 
 
 
100
  demo.launch(debug=True)
101
 
102
 
103
 
104
 
105