File size: 3,464 Bytes
e3323db
b55f92d
e3323db
e37a16e
e3323db
f7ba4c2
e3323db
 
d33646a
e3323db
 
 
 
2ded7c9
d33646a
e3323db
 
9d0e29a
e37a16e
 
 
 
 
 
 
 
 
05fd2ab
 
e37a16e
f7ba4c2
9d0e29a
 
 
d68dadc
e37a16e
f7ba4c2
 
73aa16a
312dc41
73aa16a
312dc41
73aa16a
f7ba4c2
 
05fd2ab
f7ba4c2
 
 
312dc41
 
 
 
f7ba4c2
 
 
 
d68dadc
 
e3323db
d33646a
ca37458
dc93086
 
d00f473
bcfb069
 
05fd2ab
620c42a
05fd2ab
620c42a
dc93086
d33646a
e3323db
 
d33646a
620c42a
ca37458
d33646a
4ce0852
b55f92d
 
841a099
d00f473
9e008f6
9d0e29a
 
 
 
9e008f6
05fd2ab
9e008f6
9d0e29a
05fd2ab
d00f473
05fd2ab
b55f92d
e3323db
b55f92d
f7ba4c2
 
b55f92d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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<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()