Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import numpy as np
|
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 7 |
|
| 8 |
# here we define our basic setup. we decided to store all nutrition text files inside a folder called data
|
| 9 |
# this lets us update content later just by dropping new .txt files without changing any code
|
|
@@ -155,8 +156,8 @@ def build_rag_answer(question: str) -> str:
|
|
| 155 |
return full_text
|
| 156 |
|
| 157 |
|
| 158 |
-
# this is the
|
| 159 |
-
# we ignore the
|
| 160 |
def nutri_chat(message: str, history: list) -> str:
|
| 161 |
if not message or not message.strip():
|
| 162 |
return "please type a question about healthy eating to chat with nutribud."
|
|
@@ -167,21 +168,46 @@ def nutri_chat(message: str, history: list) -> str:
|
|
| 167 |
return build_rag_answer(message)
|
| 168 |
|
| 169 |
|
| 170 |
-
# here we
|
| 171 |
-
#
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
"
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
if __name__ == "__main__":
|
| 187 |
demo.launch()
|
|
|
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
+
from gradio.themes import Soft
|
| 8 |
|
| 9 |
# here we define our basic setup. we decided to store all nutrition text files inside a folder called data
|
| 10 |
# this lets us update content later just by dropping new .txt files without changing any code
|
|
|
|
| 156 |
return full_text
|
| 157 |
|
| 158 |
|
| 159 |
+
# this is the core chat function. gradio will give us the message and the current chat history
|
| 160 |
+
# in our design, we ignore the history for retrieval because we focus on single-turn questions
|
| 161 |
def nutri_chat(message: str, history: list) -> str:
|
| 162 |
if not message or not message.strip():
|
| 163 |
return "please type a question about healthy eating to chat with nutribud."
|
|
|
|
| 168 |
return build_rag_answer(message)
|
| 169 |
|
| 170 |
|
| 171 |
+
# here we build a chat-style ui using blocks and a chatbot component
|
| 172 |
+
# this looks more like a normal chat app (similar to chatgpt) instead of a plain form
|
| 173 |
+
green_theme = Soft(primary_hue="green", neutral_hue="green")
|
| 174 |
+
|
| 175 |
+
with gr.Blocks(theme=green_theme) as demo:
|
| 176 |
+
gr.Markdown(
|
| 177 |
+
"""
|
| 178 |
+
# 🌿 nutribud
|
| 179 |
+
|
| 180 |
+
chat with nutribud about general healthy eating questions.
|
| 181 |
+
nutribud is based on public health nutrition documents and does **not** give medical advice.
|
| 182 |
+
|
| 183 |
+
try questions like:
|
| 184 |
+
- how can i eat more vegetables every day?
|
| 185 |
+
- what are some healthier drink options than soda?
|
| 186 |
+
- what does a balanced plate usually look like?
|
| 187 |
+
"""
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
chatbot = gr.Chatbot(label="nutribud chat")
|
| 191 |
+
with gr.Row():
|
| 192 |
+
msg = gr.Textbox(
|
| 193 |
+
show_label=False,
|
| 194 |
+
placeholder="type your nutrition question here and press enter...",
|
| 195 |
+
scale=4,
|
| 196 |
+
)
|
| 197 |
+
clear = gr.Button("clear chat", scale=1)
|
| 198 |
+
|
| 199 |
+
# this function connects our rag logic to the visual chat history
|
| 200 |
+
# we append the user message and nutribud’s reply as a pair so they show up as chat bubbles
|
| 201 |
+
def respond(message, history):
|
| 202 |
+
if history is None:
|
| 203 |
+
history = []
|
| 204 |
+
reply = nutri_chat(message, history)
|
| 205 |
+
history = history + [(message, reply)]
|
| 206 |
+
return "", history
|
| 207 |
+
|
| 208 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 209 |
+
clear.click(lambda: ([], ""), None, [chatbot, msg])
|
| 210 |
+
|
| 211 |
|
| 212 |
if __name__ == "__main__":
|
| 213 |
demo.launch()
|