Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import os
|
|
2 |
import gradio as gr
|
3 |
from groq import Groq
|
4 |
|
5 |
-
# β
Load API Key
|
6 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
7 |
if not GROQ_API_KEY:
|
8 |
raise ValueError("β Add your GROQ_API_KEY in Hugging Face Secrets!")
|
@@ -10,28 +10,23 @@ if not GROQ_API_KEY:
|
|
10 |
MODEL_NAME = "llama3-8b-8192"
|
11 |
|
12 |
SYSTEM_PROMPT = """
|
13 |
-
You are Dr Cat, a
|
14 |
-
You
|
15 |
-
Be
|
16 |
"""
|
17 |
|
18 |
-
#
|
19 |
-
def chat(user_input,
|
20 |
try:
|
21 |
context = ""
|
22 |
-
|
23 |
-
if
|
24 |
-
context += "\n[User uploaded a cat photo]"
|
25 |
-
if cat_video is not None:
|
26 |
-
context += "\n[User uploaded a cat video]"
|
27 |
|
28 |
client = Groq(api_key=GROQ_API_KEY)
|
29 |
-
|
30 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
31 |
for user_msg, bot_msg in history:
|
32 |
messages.append({"role": "user", "content": user_msg})
|
33 |
messages.append({"role": "assistant", "content": bot_msg})
|
34 |
-
|
35 |
full_input = user_input + context
|
36 |
messages.append({"role": "user", "content": full_input})
|
37 |
|
@@ -49,9 +44,9 @@ def chat(user_input, cat_image, cat_video, history=[]):
|
|
49 |
except Exception as e:
|
50 |
return f"β Error: {str(e)}", None, None, history
|
51 |
|
52 |
-
# β
|
53 |
with gr.Blocks(
|
54 |
-
title="π± Dr Cat
|
55 |
theme=gr.themes.Base(primary_hue="pink", secondary_hue="rose")
|
56 |
) as demo:
|
57 |
gr.Markdown(
|
@@ -59,23 +54,37 @@ with gr.Blocks(
|
|
59 |
<div style="text-align:center">
|
60 |
<img src="https://cdn-icons-png.flaticon.com/512/616/616408.png" width="80">
|
61 |
<h1>π± Dr Cat</h1>
|
62 |
-
<p
|
63 |
</div>
|
64 |
"""
|
65 |
)
|
66 |
|
67 |
chatbot = gr.Chatbot(label="π Dr Cat", height=400)
|
68 |
-
msg = gr.Textbox(label="
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
state = gr.State([])
|
74 |
|
|
|
|
|
|
|
|
|
|
|
75 |
send_btn.click(
|
76 |
fn=chat,
|
77 |
-
inputs=[msg,
|
78 |
-
outputs=[msg,
|
79 |
)
|
80 |
|
81 |
demo.launch()
|
|
|
2 |
import gradio as gr
|
3 |
from groq import Groq
|
4 |
|
5 |
+
# β
Load Groq API Key
|
6 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
7 |
if not GROQ_API_KEY:
|
8 |
raise ValueError("β Add your GROQ_API_KEY in Hugging Face Secrets!")
|
|
|
10 |
MODEL_NAME = "llama3-8b-8192"
|
11 |
|
12 |
SYSTEM_PROMPT = """
|
13 |
+
You are Dr Cat, a friendly cat expert.
|
14 |
+
You answer questions and give advice based on user text, photos or videos.
|
15 |
+
Be kind, clear and caring.
|
16 |
"""
|
17 |
|
18 |
+
# π Chat function
|
19 |
+
def chat(user_input, image_file, video_file, history=[]):
|
20 |
try:
|
21 |
context = ""
|
22 |
+
if image_file: context += "\n[User uploaded a cat photo]"
|
23 |
+
if video_file: context += "\n[User uploaded a cat video]"
|
|
|
|
|
|
|
24 |
|
25 |
client = Groq(api_key=GROQ_API_KEY)
|
|
|
26 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
27 |
for user_msg, bot_msg in history:
|
28 |
messages.append({"role": "user", "content": user_msg})
|
29 |
messages.append({"role": "assistant", "content": bot_msg})
|
|
|
30 |
full_input = user_input + context
|
31 |
messages.append({"role": "user", "content": full_input})
|
32 |
|
|
|
44 |
except Exception as e:
|
45 |
return f"β Error: {str(e)}", None, None, history
|
46 |
|
47 |
+
# β
Gradio Blocks
|
48 |
with gr.Blocks(
|
49 |
+
title="π± Dr Cat",
|
50 |
theme=gr.themes.Base(primary_hue="pink", secondary_hue="rose")
|
51 |
) as demo:
|
52 |
gr.Markdown(
|
|
|
54 |
<div style="text-align:center">
|
55 |
<img src="https://cdn-icons-png.flaticon.com/512/616/616408.png" width="80">
|
56 |
<h1>π± Dr Cat</h1>
|
57 |
+
<p>Ask me anything about your cat. Upload a photo πΈ, video π₯ or speak ποΈ!</p>
|
58 |
</div>
|
59 |
"""
|
60 |
)
|
61 |
|
62 |
chatbot = gr.Chatbot(label="π Dr Cat", height=400)
|
63 |
+
msg = gr.Textbox(label="Type your question...", placeholder="Type here...", lines=2)
|
64 |
+
|
65 |
+
with gr.Row():
|
66 |
+
# π Hidden Uploads
|
67 |
+
image = gr.File(label="Cat Photo", visible=False, file_types=["image"])
|
68 |
+
video = gr.File(label="Cat Video", visible=False, file_types=["video"])
|
69 |
+
audio = gr.Audio(source="microphone", type="filepath", visible=False)
|
70 |
+
|
71 |
+
# π Icon Buttons
|
72 |
+
photo_btn = gr.Button("πΈ", size="sm")
|
73 |
+
video_btn = gr.Button("π₯", size="sm")
|
74 |
+
mic_btn = gr.Button("ποΈ", size="sm")
|
75 |
+
send_btn = gr.Button("Ask Dr Cat", size="sm")
|
76 |
|
77 |
state = gr.State([])
|
78 |
|
79 |
+
# π Logic: show upload when icon clicked
|
80 |
+
photo_btn.click(None, None, image, js="() => document.querySelector('input[type=file]').click()")
|
81 |
+
video_btn.click(None, None, video, js="() => document.querySelector('input[type=file]').click()")
|
82 |
+
mic_btn.click(None, None, audio, js="() => {}") # Needs Whisper hook if using voice
|
83 |
+
|
84 |
send_btn.click(
|
85 |
fn=chat,
|
86 |
+
inputs=[msg, image, video, state],
|
87 |
+
outputs=[msg, image, video, chatbot],
|
88 |
)
|
89 |
|
90 |
demo.launch()
|