Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,20 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
-
# β
API
|
| 6 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 7 |
if not GROQ_API_KEY:
|
| 8 |
-
raise ValueError("β Please add GROQ_API_KEY in
|
| 9 |
|
|
|
|
| 10 |
MODEL_NAME = "llama3-8b-8192"
|
| 11 |
|
|
|
|
| 12 |
SYSTEM_PROMPT = """
|
| 13 |
You are Dr Cat, a friendly cat expert.
|
| 14 |
-
You
|
| 15 |
-
|
|
|
|
| 16 |
"""
|
| 17 |
|
| 18 |
# β
Chat logic
|
|
@@ -26,9 +29,10 @@ def chat(user_input, image_file, video_file, history=[]):
|
|
| 26 |
client = Groq(api_key=GROQ_API_KEY)
|
| 27 |
|
| 28 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 29 |
-
for
|
| 30 |
-
messages.append({"role": "user", "content":
|
| 31 |
-
messages.append({"role": "assistant", "content":
|
|
|
|
| 32 |
messages.append({"role": "user", "content": user_input + context})
|
| 33 |
|
| 34 |
response = client.chat.completions.create(
|
|
@@ -37,31 +41,37 @@ def chat(user_input, image_file, video_file, history=[]):
|
|
| 37 |
temperature=0.7,
|
| 38 |
max_tokens=512,
|
| 39 |
)
|
|
|
|
| 40 |
reply = response.choices[0].message.content
|
| 41 |
history.append((user_input, reply))
|
| 42 |
return "", None, None, history
|
| 43 |
|
| 44 |
-
# β
UI
|
| 45 |
-
with gr.Blocks(
|
|
|
|
|
|
|
|
|
|
| 46 |
gr.Markdown(
|
| 47 |
"""
|
| 48 |
-
<
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
"""
|
| 51 |
)
|
| 52 |
|
| 53 |
-
chatbot = gr.Chatbot(height=400)
|
| 54 |
-
msg = gr.Textbox(label="Question", placeholder="Type
|
| 55 |
image = gr.Image(label="πΈ Upload Cat Photo", type="filepath")
|
| 56 |
video = gr.Video(label="π₯ Upload Cat Video", type="filepath")
|
| 57 |
-
|
| 58 |
|
| 59 |
state = gr.State([])
|
| 60 |
|
| 61 |
-
|
| 62 |
-
chat,
|
| 63 |
inputs=[msg, image, video, state],
|
| 64 |
-
outputs=[msg, image, video, chatbot]
|
| 65 |
)
|
| 66 |
|
| 67 |
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
+
# β
Load GROQ API Key from Hugging Face Secrets
|
| 6 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 7 |
if not GROQ_API_KEY:
|
| 8 |
+
raise ValueError("β Please add your GROQ_API_KEY in Hugging Face Space Secrets!")
|
| 9 |
|
| 10 |
+
# β
Groq model
|
| 11 |
MODEL_NAME = "llama3-8b-8192"
|
| 12 |
|
| 13 |
+
# β
System prompt
|
| 14 |
SYSTEM_PROMPT = """
|
| 15 |
You are Dr Cat, a friendly cat expert.
|
| 16 |
+
You help cat owners with advice about cat health, food, behavior and care.
|
| 17 |
+
If the user uploads a cat photo or video, respond with relevant helpful tips.
|
| 18 |
+
Be caring, clear and easy to understand.
|
| 19 |
"""
|
| 20 |
|
| 21 |
# β
Chat logic
|
|
|
|
| 29 |
client = Groq(api_key=GROQ_API_KEY)
|
| 30 |
|
| 31 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 32 |
+
for user_msg, bot_msg in history:
|
| 33 |
+
messages.append({"role": "user", "content": user_msg})
|
| 34 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 35 |
+
|
| 36 |
messages.append({"role": "user", "content": user_input + context})
|
| 37 |
|
| 38 |
response = client.chat.completions.create(
|
|
|
|
| 41 |
temperature=0.7,
|
| 42 |
max_tokens=512,
|
| 43 |
)
|
| 44 |
+
|
| 45 |
reply = response.choices[0].message.content
|
| 46 |
history.append((user_input, reply))
|
| 47 |
return "", None, None, history
|
| 48 |
|
| 49 |
+
# β
Gradio UI
|
| 50 |
+
with gr.Blocks(
|
| 51 |
+
title="π± Dr Cat",
|
| 52 |
+
theme=gr.themes.Base(primary_hue="pink", secondary_hue="rose")
|
| 53 |
+
) as demo:
|
| 54 |
gr.Markdown(
|
| 55 |
"""
|
| 56 |
+
<div style="text-align:center">
|
| 57 |
+
<h1>π± Dr Cat</h1>
|
| 58 |
+
<p>Ask me anything about your cat! You can also upload a photo πΈ or video π₯.</p>
|
| 59 |
+
</div>
|
| 60 |
"""
|
| 61 |
)
|
| 62 |
|
| 63 |
+
chatbot = gr.Chatbot(label="π Dr Cat", height=400)
|
| 64 |
+
msg = gr.Textbox(label="Your Question", placeholder="Type your cat question...", lines=2)
|
| 65 |
image = gr.Image(label="πΈ Upload Cat Photo", type="filepath")
|
| 66 |
video = gr.Video(label="π₯ Upload Cat Video", type="filepath")
|
| 67 |
+
send_btn = gr.Button("Ask Dr Cat")
|
| 68 |
|
| 69 |
state = gr.State([])
|
| 70 |
|
| 71 |
+
send_btn.click(
|
| 72 |
+
fn=chat,
|
| 73 |
inputs=[msg, image, video, state],
|
| 74 |
+
outputs=[msg, image, video, chatbot],
|
| 75 |
)
|
| 76 |
|
| 77 |
demo.launch()
|