Fluospark128 commited on
Commit
411901c
·
verified ·
1 Parent(s): b7b75ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +160 -63
app.py CHANGED
@@ -1,64 +1,161 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import gradio as gr
3
+ import requests
4
+ import json
5
+ import speech_recognition as sr
6
+ from tempfile import NamedTemporaryFile
7
+ import pyttsx3
8
+ import logging
9
+ import time
10
+
11
+ # Set up logging
12
+ logging.basicConfig(level=logging.INFO)
13
+ logger = logging.getLogger(__name__)
14
+
15
+ # Groq API setup
16
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY", "your_key_here")
17
+ GROQ_MODEL = os.getenv("GROQ_MODEL", "mixtral-8x7b-32768")
18
+ GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
19
+
20
+ headers = {
21
+ "Authorization": f"Bearer {GROQ_API_KEY}",
22
+ "Content-Type": "application/json"
23
+ }
24
+
25
+ # Emotion options
26
+ emotion_options = {
27
+ "neutral": "Neutral or balanced mood",
28
+ "positive": "Generally positive or optimistic",
29
+ "happy": "Feeling joy or happiness",
30
+ "excited": "Feeling enthusiastic or energetic",
31
+ "sad": "Feeling down or unhappy",
32
+ "angry": "Feeling frustrated or irritated",
33
+ "negative": "Generally negative or pessimistic",
34
+ "anxious": "Feeling worried or nervous"
35
+ }
36
+
37
+ # Initialize conversation history
38
+ conversation_history = []
39
+
40
+ # Transcribe audio to text
41
+ def transcribe_audio(audio_path):
42
+ recognizer = sr.Recognizer()
43
+ try:
44
+ with sr.AudioFile(audio_path) as source:
45
+ audio_data = recognizer.record(source)
46
+ transcription = recognizer.recognize_google(audio_data)
47
+ return transcription
48
+ except Exception as e:
49
+ logger.error(f"Audio transcription failed: {e}")
50
+ return ""
51
+
52
+ # Get Groq response
53
+ def get_groq_response(prompt, history):
54
+ messages = [{"role": "system", "content": prompt}]
55
+ for message in history:
56
+ if message.startswith("User: "):
57
+ messages.append({"role": "user", "content": message[6:]})
58
+ elif message.startswith("AI: "):
59
+ messages.append({"role": "assistant", "content": message[4:]})
60
+ data = {
61
+ "model": GROQ_MODEL,
62
+ "messages": messages,
63
+ "temperature": 0.7,
64
+ "max_tokens": 1024
65
+ }
66
+ try:
67
+ response = requests.post(GROQ_API_URL, headers=headers, json=data)
68
+ return response.json()["choices"][0]["message"]["content"]
69
+ except Exception as e:
70
+ logger.error(f"Groq API error: {e}")
71
+ return "Error contacting AI."
72
+
73
+ # Generate speech using pyttsx3 and save to WAV
74
+ def generate_speech(text):
75
+ try:
76
+ tts = pyttsx3.init()
77
+ temp_file = NamedTemporaryFile(delete=False, suffix=".wav")
78
+ temp_path = temp_file.name
79
+ tts.save_to_file(text, temp_path)
80
+ tts.runAndWait()
81
+ time.sleep(1) # Ensure file is saved
82
+ return temp_path
83
+ except Exception as e:
84
+ logger.error(f"TTS error: {e}")
85
+ return None
86
+
87
+ # Main interaction
88
+ def chat_with_ai(audio, text_input, emotion, history):
89
+ global conversation_history
90
+ user_text = text_input or ""
91
+ if audio:
92
+ transcription = transcribe_audio(audio)
93
+ if transcription:
94
+ user_text = transcription
95
+ else:
96
+ return "Couldn't understand the audio.", None, history
97
+ if not user_text.strip():
98
+ return "No input provided.", None, history
99
+
100
+ conversation_history.append(f"User: {user_text}")
101
+ recent_messages = conversation_history[-20:]
102
+
103
+ prompt = f"""You are an empathetic AI assistant. The user is feeling {emotion} ({emotion_options[emotion]}). Respond supportively."""
104
+ ai_response = get_groq_response(prompt, recent_messages)
105
+ conversation_history.append(f"AI: {ai_response}")
106
+
107
+ audio_path = generate_speech(ai_response)
108
+ return ai_response, audio_path, history + [[user_text, ai_response]]
109
+
110
+ def clear_conversation():
111
+ global conversation_history
112
+ conversation_history = []
113
+ return [], None, None, "Conversation cleared."
114
+
115
+ # Gradio Interface
116
+ iface = gr.Blocks()
117
+
118
+ with iface:
119
+ gr.Markdown("# Mind AID AI Assistant")
120
+ gr.Markdown("Talk or type to the AI assistant. Your emotional state helps tailor the response.")
121
+
122
+ with gr.Row():
123
+ with gr.Column(scale=3):
124
+ emotion = gr.Dropdown(label="Your emotion?", choices=list(emotion_options.keys()), value="neutral")
125
+ emotion_description = gr.Markdown("**Current mood:** Neutral")
126
+ def update_emotion_desc(em):
127
+ return f"**Current mood:** {emotion_options.get(em, 'Unknown')}"
128
+ emotion.change(fn=update_emotion_desc, inputs=[emotion], outputs=[emotion_description])
129
+ with gr.Column(scale=1):
130
+ clear_btn = gr.Button("Clear Conversation")
131
+ status = gr.Textbox(label="Status")
132
+
133
+ chat_history = gr.Chatbot(label="Chat History", height=300)
134
+
135
+ with gr.Row():
136
+ text_input = gr.Textbox(label="Type your message", lines=2)
137
+ audio_input = gr.Audio(label="Or speak", type="filepath", sources=["microphone"])
138
+
139
+ output_audio = gr.Audio(label="AI Voice Response")
140
+ submit_btn = gr.Button("Send", variant="primary")
141
+
142
+ submit_btn.click(
143
+ fn=chat_with_ai,
144
+ inputs=[audio_input, text_input, emotion, chat_history],
145
+ outputs=[status, output_audio, chat_history]
146
+ )
147
+
148
+ text_input.submit(
149
+ fn=chat_with_ai,
150
+ inputs=[audio_input, text_input, emotion, chat_history],
151
+ outputs=[status, output_audio, chat_history]
152
+ )
153
+
154
+ clear_btn.click(
155
+ fn=clear_conversation,
156
+ inputs=[],
157
+ outputs=[chat_history, audio_input, text_input, status]
158
+ )
159
+
160
+ # Launch (MUST be outside if __name__ == "__main__")
161
+ iface.launch()