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![Cat Photo]({image_file})" if video_file: context += f"\n" 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( """

🐱 Dr Cat

Your AI Cat Care Companion — Ask anything about your furry friend 🐾

""" ) 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()