Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
from groq import Groq | |
import speech_recognition as sr | |
# β API Key | |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY") | |
if not GROQ_API_KEY: | |
raise ValueError("β Please add your GROQ_API_KEY in Hugging Face Space Secrets!") | |
MODEL_NAME = "llama3-8b-8192" | |
SYSTEM_PROMPT = """ | |
You are Dr Cat, a friendly cat expert. | |
You help cat owners with advice about cat health, food, behavior and care. | |
""" | |
# β Speech recognition | |
def recognize_speech(audio_file): | |
if audio_file is None: | |
return gr.update(value="") | |
recognizer = sr.Recognizer() | |
with sr.AudioFile(audio_file) as source: | |
audio = recognizer.record(source) | |
try: | |
text = recognizer.recognize_google(audio) | |
return gr.update(value=text) | |
except: | |
return gr.update(value="Could not recognize speech.") | |
# β Show Audio on mic click | |
def show_audio(): | |
return gr.update(visible=True) | |
# β Chat logic β FINAL OUTPUTS: 5 | |
def chat(user_input, image_file, video_file, history): | |
if history is None: | |
history = [] | |
context = user_input | |
if image_file: | |
context += f"\n" | |
if video_file: | |
context += f"\n<video controls width='250'><source src='{video_file}' type='video/mp4'></video>" | |
messages = [{"role": "system", "content": SYSTEM_PROMPT}] + history | |
messages.append({"role": "user", "content": context}) | |
client = Groq(api_key=GROQ_API_KEY) | |
response = client.chat.completions.create( | |
model=MODEL_NAME, | |
messages=messages, | |
temperature=0.7, | |
max_tokens=512, | |
) | |
reply = response.choices[0].message.content | |
messages.append({"role": "assistant", "content": reply}) | |
return "", None, None, messages, messages # β 5 Outputs! | |
with gr.Blocks( | |
title="π± Dr Cat | Your AI Cat Care Companion", | |
theme=gr.themes.Soft(primary_hue="pink", secondary_hue="rose"), | |
css=""" | |
body { background: #121212; color: #f5f5f5; } | |
.gr-button, .gr-upload-button { background: #ff69b4; color: white; border-radius: 50%; width: 40px; height: 40px; padding: 0; font-size: 20px; } | |
.gr-button:hover, .gr-upload-button:hover { background: #ff85c1; } | |
.gr-textbox textarea { border-radius: 30px; background: #1e1e1e; color: #f5f5f5; font-size: 16px; } | |
.gr-chatbot { border-radius: 16px; background: #1e1e1e; } | |
h1 { font-size: 42px; } | |
p { font-size: 18px; color: #ccc; } | |
""" | |
) as demo: | |
gr.Markdown( | |
""" | |
<div style="text-align:center"> | |
<h1>π± Dr Cat</h1> | |
<p>Your AI Cat Care Companion β Ask anything about your furry friend πΎ</p> | |
</div> | |
""" | |
) | |
chatbot = gr.Chatbot(label="π Dr Cat", height=500, type="messages") | |
with gr.Row(): | |
msg = gr.Textbox(placeholder="Ask Dr Cat about your kitty...", scale=8) | |
image = gr.UploadButton("πΈ", file_types=["image"], scale=1) | |
video = gr.UploadButton("π₯", file_types=["video"], scale=1) | |
mic_btn = gr.Button("ποΈ", scale=1) | |
audio = gr.Audio(visible=False, type="filepath") | |
mic_btn.click(show_audio, None, audio) | |
audio.change(recognize_speech, inputs=audio, outputs=msg) | |
send_btn = gr.Button("π¬ Send") | |
state = gr.State([]) | |
msg.submit(chat, [msg, image, video, state], [msg, image, video, chatbot, state]) | |
send_btn.click(chat, [msg, image, video, state], [msg, image, video, chatbot, state]) | |
demo.launch() | |