Nidakanwal commited on
Commit
d33646a
Β·
verified Β·
1 Parent(s): 312dc41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -17
app.py CHANGED
@@ -2,17 +2,20 @@ import os
2
  import gradio as gr
3
  from groq import Groq
4
 
5
- # βœ… API KEY
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 your Hugging Face Space Secrets!")
9
 
 
10
  MODEL_NAME = "llama3-8b-8192"
11
 
 
12
  SYSTEM_PROMPT = """
13
  You are Dr Cat, a friendly cat expert.
14
- You answer questions about cat health, behavior, diet, and care.
15
- You also look at photos or videos the user uploads.
 
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 u, b in history:
30
- messages.append({"role": "user", "content": u})
31
- messages.append({"role": "assistant", "content": b})
 
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() as demo:
 
 
 
46
  gr.Markdown(
47
  """
48
- <h1>🐱 Dr Cat</h1>
49
- <p>Type your question. Optionally upload photo πŸ“Έ or video πŸŽ₯ of your cat.</p>
 
 
50
  """
51
  )
52
 
53
- chatbot = gr.Chatbot(height=400)
54
- msg = gr.Textbox(label="Question", placeholder="Type here...")
55
  image = gr.Image(label="πŸ“Έ Upload Cat Photo", type="filepath")
56
  video = gr.Video(label="πŸŽ₯ Upload Cat Video", type="filepath")
57
- btn = gr.Button("Ask Dr Cat")
58
 
59
  state = gr.State([])
60
 
61
- btn.click(
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()