AnaviJoshi commited on
Commit
f257aba
·
verified ·
1 Parent(s): 407988f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -19
app.py CHANGED
@@ -58,42 +58,119 @@ def get_top_chunks(query): # store a function that gets the most relevant_info a
58
  client = InferenceClient("google/gemma-3-27b-it")
59
 
60
  def respond(message, history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  #use created variable for relevant info\
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
  #change HOW the bot responds
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
  #theme = gr.themes.Origin(primary_hue="orange",secondary_hue="indigo", neutral_hue="teal")
80
 
81
 
82
  #chatbot = gr.ChatInterface(respond, type = "messages", theme)
83
 
84
- with gr.Blocks(theme=gr.themes.Origin(primary_hue="orange", secondary_hue="indigo", neutral_hue="teal")) as demo:
85
- gr.Markdown("## 🐶 Scooby the Pet Expert")
86
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  with gr.Row():
88
- text_input = gr.Textbox(label="Ask Scooby a question about your pet")
89
- image_input = gr.Image(type="pil", label="Upload an image of your pet (optional)")
90
- gr.Image(value="logo.jpg", width=200, show_label=False, show_share_button = False, show_download_button = False, container=False)
 
 
 
 
 
91
 
92
- output = gr.Textbox(label="Scooby's Answer")
93
 
94
- submit_btn = gr.Button("Ask Scooby")
95
 
96
- submit_btn.click(fn=respond, inputs=[text_input, image_input], outputs=output)
97
 
98
  demo.launch(debug=True)
99
 
 
58
  client = InferenceClient("google/gemma-3-27b-it")
59
 
60
  def respond(message, history):
61
+ # Get relevant information from knowledge base
62
+ relevant_chunks = get_top_chunks(message)
63
+ context = "\n".join(relevant_chunks)
64
+
65
+ # Build the system message including context
66
+ system_message = (
67
+ "You are a helpful chatbot named Scooby — kind of like the cartoon character but not too much. "
68
+ "You specialize in pets and their diets and ONLY answer questions about pets.\n\n"
69
+ "Relevant Info:\n" + context
70
+ )
71
+
72
+ # Construct chat messages
73
+ messages = [{"role": "system", "content": system_message}]
74
+ if history:
75
+ messages.extend(history)
76
+ messages.append({"role": "user", "content": message})
77
+
78
+ # Streaming response
79
+ response = ""
80
+ for chunk in client.chat_completion(messages, max_tokens=2500, stream=True):
81
+ token = chunk.choices[0].delta.content or ""
82
+ response += token
83
+ yield response
84
+
85
+
86
+ #def respond(message, history):
87
  #use created variable for relevant info\
88
 
89
+ # 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"
90
+ # messages = [{"role": "system", "content": system_message}]
91
 
92
 
93
  #change HOW the bot responds
94
 
95
+ # if history:
96
+ # messages.extend(history)
97
 
98
+ #messages.append({"role": "user", "content": message})
99
+ #response = ""
100
+ #for messages in client.chat_completion(messages,max_tokens = 2500, stream = True):
101
+ # token = messages.choices[0].delta.content
102
+ # response += token
103
+ # yield response
104
 
105
  #theme = gr.themes.Origin(primary_hue="orange",secondary_hue="indigo", neutral_hue="teal")
106
 
107
 
108
  #chatbot = gr.ChatInterface(respond, type = "messages", theme)
109
 
110
+ #with gr.Blocks(theme=gr.themes.Origin(primary_hue="orange", secondary_hue="indigo", neutral_hue="teal")) as demo:
111
+ # gr.Markdown("## 🐶 Scooby the Pet Expert")
112
+
113
+ # with gr.Row():
114
+ # text_input = gr.Textbox(label="Ask Scooby a question about your pet")
115
+ # image_input = gr.Image(type="pil", label="Upload an image of your pet (optional)")
116
+ # gr.Image(value="logo.jpg", width=200, show_label=False, show_share_button = False, show_download_button = False, container=False)
117
+
118
+ # output = gr.Textbox(label="Scooby's Answer")
119
+
120
+ #submit_btn = gr.Button("Ask Scooby")
121
+
122
+ #submit_btn.click(fn=respond, inputs=[text_input, image_input], outputs=output)
123
+
124
+ welcome_message = """
125
+ # Welcome to ScoobyAI!
126
+ ## ScoobyAI is perfect for pet owners! We know it can be overwhelming to take care of a pet, so our chatbot Scooby is perfect for you!
127
+ """
128
+
129
+ topics = """
130
+ ### Scooby is happy to help you with these topics!
131
+ - Feeding Schedules
132
+ - Pet health issues
133
+ - Food Recipes for Pets
134
+ """
135
+ created_by_messages = """
136
+ ### Created by Anavi, Ananya, Nithyla
137
+ """
138
+ theme = gr.themes.Monochrome(
139
+ primary_hue="teal",
140
+ secondary_hue="orange",
141
+ ).set(
142
+ background_fill_primary='*primary_200',
143
+ background_fill_primary_dark='*primary_200',
144
+ background_fill_secondary='*secondary_300',
145
+ background_fill_secondary_dark='*secondary_300',
146
+ border_color_accent='*secondary_200',
147
+ border_color_accent_dark='*secondary_600',
148
+ border_color_accent_subdued='*secondary_200',
149
+ border_color_primary='*secondary_300',
150
+ block_border_color='*secondary_200',
151
+ button_primary_background_fill='*secondary_300',
152
+ button_primary_background_fill_dark='*secondary_300'
153
+ )
154
+
155
+
156
+
157
+
158
+ with gr.Blocks(theme=theme) as demo:
159
+ gr.Image("logo.jpg", container=False, show_share_button=False, show_download_button=False, label="output", show_label=True, elem_id="output_image")
160
+ gr.Markdown(welcome_message)
161
+
162
  with gr.Row():
163
+ with gr.Column():
164
+ gr.Markdown(topics)
165
+ with gr.Column():
166
+ question = gr.Textbox(label="Your question", placeholder="What do you want to ask Scooby?")
167
+ answer = gr.Textbox(label="Scooby's Response", placeholder="Scooby will respond here...", interactive=False, lines=17)
168
+ submit_button = gr.Button("Submit")
169
+ submit_button.click(fn=respond, inputs=question, outputs=answer, stream=True)
170
+
171
 
 
172
 
 
173
 
 
174
 
175
  demo.launch(debug=True)
176