Nidakanwal commited on
Commit
2ded7c9
Β·
verified Β·
1 Parent(s): fa43980

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -22
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 caring and expert cat advisor.
14
- You can give advice based on user questions, images, or videos they upload about their cats.
15
- Be clear, kind and helpful.
16
  """
17
 
18
- # 🐱 Chat function
19
- def chat(user_input, cat_image, cat_video, history=[]):
20
  try:
21
  context = ""
22
-
23
- if cat_image is not None:
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
- # βœ… UI blocks
53
  with gr.Blocks(
54
- title="🐱 Dr Cat - Cat Care with Pics & Voice",
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><b>Welcome!</b> Upload your cat’s photo or video, or speak/type your question.</p>
63
  </div>
64
  """
65
  )
66
 
67
  chatbot = gr.Chatbot(label="🐈 Dr Cat", height=400)
68
- msg = gr.Textbox(label="Your Question", placeholder="Type your cat question...", lines=2)
69
- img_input = gr.Image(label="Upload Cat Photo", type="filepath")
70
- vid_input = gr.Video(label="Upload Cat Video (optional)")
71
- send_btn = gr.Button("Ask Dr Cat")
 
 
 
 
 
 
 
 
 
72
 
73
  state = gr.State([])
74
 
 
 
 
 
 
75
  send_btn.click(
76
  fn=chat,
77
- inputs=[msg, img_input, vid_input, state],
78
- outputs=[msg, img_input, vid_input, chatbot],
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()