AnaviJoshi commited on
Commit
d103e1b
·
verified ·
1 Parent(s): 0d2676c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -43
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  from sentence_transformers import SentenceTransformer
3
  import torch
4
  from huggingface_hub import InferenceClient
5
- from sentence_transformers import util
6
  #import os
7
 
8
  #HF_TOKEN = os.environ.get("HF_TOKEN")
@@ -59,58 +58,26 @@ def get_top_chunks(query): # store a function that gets the most relevant_info a
59
  client = InferenceClient("google/gemma-3-27b-it")
60
 
61
 
62
- #def respond(message, history):
63
- #
64
- # system_message = "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"
65
- # messages = [{"role": "system", "content": system_message}]
66
-
67
-
68
-
69
- # if history:
70
- # messages.extend(history)
71
-
72
- # messages.append({"role": "user", "content": message})
73
- #response = ""
74
- #for messages in client.chat_completion(messages,max_tokens = 2500, stream = True):
75
- # token = messages.choices[0].delta.content
76
- # response += token
77
- # yield response
78
-
79
  def respond(message, history):
80
- # Step 1: Embed the user's question
81
- message_embedding = model.encode(message, convert_to_tensor=True)
82
-
83
- # Step 2: Calculate similarity with knowledge chunks
84
- scores = util.cos_sim(message_embedding, chunk_embeddings)[0]
85
- top_k = 3 # You can adjust how many chunks you want to include
86
- top_results = torch.topk(scores, k=top_k)
87
-
88
- # Step 3: Retrieve the top relevant knowledge chunks
89
- retrieved_knowledge = "\n".join([chunks[i] for i in top_results.indices])
90
-
91
- # Step 4: Build system message with retrieved knowledge
92
- system_message = (
93
- "You are a helpful chatbot named Scooby, kind of like the cartoon character but not too much.
94
- You know a lot about pets and their diets, and you only answer questions about pets.
95
- Use the following relevant knowledge to help answer the user's question"
96
- + retrieved_knowledge
97
- )
98
 
99
- # Step 5: Compose message list for the LLM
100
- messages = [{"role": "system", "content": system_message}]
101
 
 
 
102
  if history:
103
  messages.extend(history)
104
-
105
  messages.append({"role": "user", "content": message})
106
-
107
- # Step 6: Stream response
108
  response = ""
109
- for chunk in client.chat_completion(messages, max_tokens=2500, stream=True):
110
- token = chunk.choices[0].delta.content
111
  response += token
112
  yield response
113
 
 
 
 
114
  #theme = gr.themes.Origin(primary_hue="orange",secondary_hue="indigo", neutral_hue="teal")
115
 
116
 
 
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")
 
58
  client = InferenceClient("google/gemma-3-27b-it")
59
 
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  def respond(message, history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ system_message = "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"
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.Origin(primary_hue="orange",secondary_hue="indigo", neutral_hue="teal")
82
 
83