AhmadFareedKhan commited on
Commit
cac2464
β€’
1 Parent(s): b93a002

Rename assistant.py to app.py

Browse files
Files changed (1) hide show
  1. 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
- # assistant single app
8
-
9
- # Load and set environment variables
10
- load_dotenv(".env")
11
- api_key = os.getenv("OPENAI_API_KEY")
12
- os.environ['SPEECH_KEY'] = '3ca965cb089e415d85a780e0ce40a3cf'
13
- os.environ['SPEECH_REGION'] = 'eastus'
14
- client = OpenAI(api_key=api_key)
15
-
16
- def recognize_from_microphone(file_info):
17
- if not file_info:
18
- return "", "No audio file received."
19
- file_path = file_info
20
- if not os.path.exists(file_path):
21
- return "", f"File not found: {file_path}"
22
- speech_config = speechsdk.SpeechConfig(subscription=os.environ['SPEECH_KEY'], region=os.environ['SPEECH_REGION'])
23
- speech_config.speech_recognition_language = "en-US"
24
- try:
25
- audio_config = speechsdk.audio.AudioConfig(filename=file_path)
26
- speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
27
- result = speech_recognizer.recognize_once()
28
- if result.reason == speechsdk.ResultReason.RecognizedSpeech:
29
- return result.text, ""
30
- elif result.reason == speechsdk.ResultReason.NoMatch:
31
- return "", "No speech could be recognized."
32
- elif result.reason == speechsdk.ResultReason.Canceled:
33
- return "", f"Speech Recognition canceled: {result.cancellation_details.reason}."
34
- except Exception as e:
35
- return "", f"Error during speech recognition: {str(e)}"
36
-
37
- return "", "Unexpected error during speech recognition."
38
-
39
-
40
- def chatbot_response(user_input="", audio_input=None):
41
- transcription, error = recognize_from_microphone(audio_input) if audio_input else ("", "")
42
- if transcription:
43
- user_input = transcription
44
- if not user_input.strip():
45
- return error or "Please provide some input or speak into the microphone.", ""
46
- try:
47
- completion = client.chat.completions.create(
48
- model="gpt-3.5-turbo",
49
- messages=[
50
- {"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."},
51
- {"role": "user", "content": user_input},
52
- ]
53
- )
54
- response = completion.choices[0].message.content
55
- return transcription, response
56
- except Exception as e:
57
- return transcription, f"An error occurred during response generation: {str(e)}"
58
-
59
-
60
- app = gr.Interface(
61
- fn=chatbot_response,
62
- inputs=[gr.Textbox(lines=5, placeholder="Enter your emergency nutrition query here...", label="Input Here"),
63
- gr.Audio(type="filepath", label="Record your question")],
64
- outputs=[gr.Text(label="Transcription"), gr.Text(label="Response")],
65
- title="Emergency Assistance",
66
- description="To better assist you, could you explain what led to this emergency?"
67
- )
68
-
69
- if __name__ == "__main__":
70
- app.launch(share=False)
 
 
 
 
 
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)