Spaces:
Sleeping
Sleeping
AhmadFareedKhan
commited on
Commit
β’
cac2464
1
Parent(s):
b93a002
Rename assistant.py to app.py
Browse files- assistant.py β app.py +74 -70
assistant.py β app.py
RENAMED
@@ -1,70 +1,74 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from openai import OpenAI
|
3 |
-
import os
|
4 |
-
from dotenv import load_dotenv
|
5 |
-
import azure.cognitiveservices.speech as speechsdk
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
os.environ['
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
speech_config
|
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 |
-
title="Emergency Assistance",
|
66 |
-
description="To better assist you, could you explain what led to this emergency?"
|
67 |
-
)
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from openai import OpenAI
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
import azure.cognitiveservices.speech as speechsdk
|
6 |
+
import wave
|
7 |
+
|
8 |
+
# Loads and set environment variables
|
9 |
+
load_dotenv(".env")
|
10 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
11 |
+
speech_key = os.getenv("speech_key")
|
12 |
+
os.environ['SPEECH_REGION'] = 'eastus'
|
13 |
+
client = OpenAI(api_key=api_key)
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
def synthesize_speech(text, filename="output.wav"):
|
18 |
+
""" Converts text to speech and saves it to a WAV file. """
|
19 |
+
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=os.environ['SPEECH_REGION'])
|
20 |
+
speech_config.speech_synthesis_voice_name = "en-US-AvaMultilingualNeural"
|
21 |
+
audio_config = speechsdk.audio.AudioConfig(filename=filename)
|
22 |
+
|
23 |
+
synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
|
24 |
+
result = synthesizer.speak_text_async(text).get()
|
25 |
+
|
26 |
+
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
|
27 |
+
print(f"Speech synthesized for text [{text}] and saved to {filename}")
|
28 |
+
else:
|
29 |
+
print(f"Failed to synthesize speech for text [{text}]")
|
30 |
+
return filename
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
def emergency_assistance(query):
|
37 |
+
if not query.strip():
|
38 |
+
return "Please provide a query for emergency assistance."
|
39 |
+
try:
|
40 |
+
completion = client.chat.completions.create(
|
41 |
+
model="gpt-3.5-turbo",
|
42 |
+
messages=[
|
43 |
+
{"role": "system", "content": "As an AI serving as an emergency nutrition advisor, your objective is to provide prompt and accurate nutritional guidance in urgent situations. When users present their concerns, you should deliver tailored advice that addresses the critical aspects of their nutritional needs quickly and effectively. Focus on offering clear, practical, and context-specific solutions to ensure their immediate dietary requirements are met."},
|
44 |
+
{"role": "user", "content": query},
|
45 |
+
]
|
46 |
+
)
|
47 |
+
response = completion.choices[0].message.content
|
48 |
+
except Exception as e:
|
49 |
+
return f"An error occurred: {str(e)}"
|
50 |
+
# After generating response:
|
51 |
+
if response:
|
52 |
+
audio_path = synthesize_speech(response)
|
53 |
+
return response, audio_path # Return both response text and audio path
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
interface2 = gr.Interface(
|
59 |
+
fn=emergency_assistance,
|
60 |
+
inputs=[gr.Textbox(lines=10, label="Query", placeholder="Enter your emergency nutrition query here...")],
|
61 |
+
outputs=[
|
62 |
+
gr.Text(lines=10, label="Response"),
|
63 |
+
gr.Audio(label="Listen to Response") # New audio output for the synthesized speech
|
64 |
+
],
|
65 |
+
title="Emergency Assistance",
|
66 |
+
description="To better assist you, could you explain what led to this emergency?"
|
67 |
+
)
|
68 |
+
|
69 |
+
|
70 |
+
# Combined interface with tabs
|
71 |
+
app = gr.TabbedInterface([interface2], ["Nutrition Consultant", "Emergency Assistance"], title="HealthyBytes: Your AI Nutrition Consultant")
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
app.launch(share=False)
|