Spaces:
Sleeping
Sleeping
AhmadFareedKhan
commited on
Commit
•
0917861
1
Parent(s):
2b45ded
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from openai import OpenAI
|
3 |
import os
|
|
|
|
|
4 |
from dotenv import load_dotenv
|
5 |
-
|
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 |
|
18 |
-
|
19 |
|
20 |
def emergency_assistance(query):
|
21 |
if not query.strip():
|
@@ -29,19 +137,40 @@ def emergency_assistance(query):
|
|
29 |
]
|
30 |
)
|
31 |
response = completion.choices[0].message.content
|
32 |
-
return response
|
33 |
except Exception as e:
|
34 |
return f"An error occurred: {str(e)}"
|
|
|
|
|
|
|
|
|
35 |
|
36 |
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
interface2 = gr.Interface(
|
41 |
fn=emergency_assistance,
|
42 |
inputs=[gr.Textbox(lines=10, label="Query", placeholder="Enter your emergency nutrition query here...")],
|
43 |
outputs=[
|
44 |
gr.Text(lines=10, label="Response"),
|
|
|
45 |
],
|
46 |
title="Emergency Assistance",
|
47 |
description="To better assist you, could you explain what led to this emergency?"
|
@@ -49,7 +178,7 @@ interface2 = gr.Interface(
|
|
49 |
|
50 |
|
51 |
# Combined interface with tabs
|
52 |
-
app = gr.TabbedInterface([interface2], ["Nutrition Consultant", "Emergency Assistance"], title="HealthyBytes: Your AI Nutrition Consultant")
|
53 |
|
54 |
if __name__ == "__main__":
|
55 |
-
app.launch(share=False)
|
|
|
1 |
+
from botocore.exceptions import BotoCoreError, ClientError
|
2 |
+
from contextlib import closing
|
3 |
+
import os
|
4 |
+
import urllib
|
5 |
+
import boto3
|
6 |
import gradio as gr
|
7 |
from openai import OpenAI
|
8 |
import os
|
9 |
+
import time
|
10 |
+
import json
|
11 |
from dotenv import load_dotenv
|
12 |
+
|
|
|
13 |
|
14 |
# Loads and set environment variables
|
15 |
load_dotenv(".env")
|
16 |
api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
17 |
client = OpenAI(api_key=api_key)
|
18 |
+
access_key = os.getenv('access_key')
|
19 |
+
secret_key = os.getenv('secret_key')
|
20 |
+
|
21 |
+
|
22 |
+
def recognize_from_microphone(file_info):
|
23 |
+
if not file_info:
|
24 |
+
return "No audio file received.", ""
|
25 |
+
file_path = file_info
|
26 |
+
print(f"File path received: {file_path}")
|
27 |
+
|
28 |
+
# Check file existence
|
29 |
+
if not os.path.exists(file_path):
|
30 |
+
return f"File not found: {file_path}", ""
|
31 |
+
|
32 |
+
# Configuring Amazon Transcribe
|
33 |
+
transcribe_client = boto3.client('transcribe', region_name='us-east-1', aws_access_key_id=access_key, aws_secret_access_key=secret_key)
|
34 |
+
s3_client = boto3.client('s3', region_name='us-west-2', aws_access_key_id=access_key, aws_secret_access_key=secret_key)
|
35 |
+
bucket_name = 'nutrition-bot' # Specify your S3 bucket name
|
36 |
+
object_name = os.path.basename(file_path)
|
37 |
+
|
38 |
+
# Upload file to S3
|
39 |
+
s3_client.upload_file(file_path, bucket_name, object_name)
|
40 |
+
job_name = f"TranscriptionJob-{int(time.time())}"
|
41 |
+
job_uri = f"s3://{bucket_name}/{object_name}"
|
42 |
+
|
43 |
+
# Start transcription job
|
44 |
+
transcribe_client.start_transcription_job(
|
45 |
+
TranscriptionJobName=job_name,
|
46 |
+
Media={'MediaFileUri': job_uri},
|
47 |
+
MediaFormat='mp3', # or your file format, e.g., wav
|
48 |
+
LanguageCode='en-US'
|
49 |
+
)
|
50 |
+
|
51 |
+
# Checking job status
|
52 |
+
while True:
|
53 |
+
status = transcribe_client.get_transcription_job(TranscriptionJobName=job_name)
|
54 |
+
if status['TranscriptionJob']['TranscriptionJobStatus'] in ['COMPLETED', 'FAILED']:
|
55 |
+
break
|
56 |
+
time.sleep(5)
|
57 |
+
|
58 |
+
# Process the transcription result
|
59 |
+
if status['TranscriptionJob']['TranscriptionJobStatus'] == 'COMPLETED':
|
60 |
+
transcript_uri = status['TranscriptionJob']['Transcript']['TranscriptFileUri']
|
61 |
+
transcript_response = urllib.request.urlopen(transcript_uri)
|
62 |
+
transcript_data = json.load(transcript_response)
|
63 |
+
transcript_text = transcript_data['results']['transcripts'][0]['transcript']
|
64 |
+
return transcript_text, ""
|
65 |
+
return "Failed to transcribe audio.", ""
|
66 |
+
|
67 |
+
|
68 |
+
def synthesize_speech(text, filename="output.mp3"):
|
69 |
+
"""Converts text to speech using Amazon Polly and saves it to an MP3 file."""
|
70 |
+
# Create a Polly client
|
71 |
+
polly_client = boto3.client('polly', region_name='us-east-1', aws_access_key_id=access_key, aws_secret_access_key=secret_key)
|
72 |
+
|
73 |
+
# Synthesize speech
|
74 |
+
response = polly_client.synthesize_speech(
|
75 |
+
Text=text,
|
76 |
+
OutputFormat='mp3', # MP3 output format
|
77 |
+
VoiceId='Salli' # Using Joanna voice, you can choose another
|
78 |
+
)
|
79 |
+
|
80 |
+
# Accessing the audio stream from the response
|
81 |
+
if "AudioStream" in response:
|
82 |
+
with open(filename, 'wb') as file:
|
83 |
+
file.write(response['AudioStream'].read())
|
84 |
+
print(f"Speech synthesized for text [{text}] and saved to {filename}")
|
85 |
+
else:
|
86 |
+
print(f"Failed to synthesize speech for text [{text}]")
|
87 |
+
|
88 |
+
return filename
|
89 |
+
|
90 |
+
|
91 |
|
92 |
+
def chatbot_response(user_input="", gender=None, plan_type=None, weight=None, height=None, audio_input=None):
|
93 |
+
transcription, response = "", "" # Initialize variables for transcription and response
|
94 |
+
error_message = "" # Initialize error_message at the start of the function
|
95 |
|
96 |
+
if audio_input:
|
97 |
+
transcription, error = recognize_from_microphone(audio_input)
|
98 |
+
if error:
|
99 |
+
error_message = error # Capture the error to return it properly
|
100 |
+
else:
|
101 |
+
user_input = transcription # Use the transcription if there's no error
|
102 |
|
103 |
+
# Check if there's user input or transcription, and there's no error message
|
104 |
+
if not user_input.strip() and not transcription.strip() and not error_message:
|
105 |
+
error_message = "Please provide audio input or type your question."
|
106 |
+
|
107 |
+
if error_message:
|
108 |
+
return error_message, "" # Return the error with an empty second value
|
109 |
+
|
110 |
+
# Process user_input as before (assuming previous code handled it)
|
111 |
+
detailed_input = f"User details - Gender: {gender}, Plan Type: {plan_type}, Weight: {weight} kg, Height: {height} cm. Question: {user_input}"
|
112 |
+
try:
|
113 |
+
completion = client.chat.completions.create(
|
114 |
+
model="gpt-3.5-turbo",
|
115 |
+
messages=[
|
116 |
+
{"role": "system", "content": "You are a nutrition consultant AI, capable of providing natural diet plans and emergency assistance based on user inputs."},
|
117 |
+
{"role": "user", "content": detailed_input},
|
118 |
+
]
|
119 |
+
)
|
120 |
+
response = completion.choices[0].message.content
|
121 |
+
if response:
|
122 |
+
audio_path = synthesize_speech(response)
|
123 |
+
return transcription, response, audio_path # Return audio path along with text and transcription
|
124 |
+
except Exception as e:
|
125 |
+
return transcription, f"An error occurred during response generation: {str(e)}" # Return both values even in case of an exception
|
126 |
|
|
|
127 |
|
128 |
def emergency_assistance(query):
|
129 |
if not query.strip():
|
|
|
137 |
]
|
138 |
)
|
139 |
response = completion.choices[0].message.content
|
|
|
140 |
except Exception as e:
|
141 |
return f"An error occurred: {str(e)}"
|
142 |
+
# After generating response:
|
143 |
+
if response:
|
144 |
+
audio_path = synthesize_speech(response)
|
145 |
+
return response, audio_path # Return both response text and audio path
|
146 |
|
147 |
|
148 |
+
# Adjust Gradio interfaces to include audio output
|
149 |
+
interface1 = gr.Interface(
|
150 |
+
fn=chatbot_response,
|
151 |
+
inputs=[
|
152 |
+
gr.Textbox(lines=5, label="Input Here", placeholder="Type or say your question here..."),
|
153 |
+
gr.Radio(choices=["Male", "Female", "Other"], label="Gender"),
|
154 |
+
gr.Radio(choices=["Weight Gain", "Weight Loss"], label="Plan Type"),
|
155 |
+
gr.Number(label="Weight (kg)", info="Enter your weight in kg"),
|
156 |
+
gr.Number(label="Height (cm)", info="Enter your height in cm"),
|
157 |
+
gr.Audio(type="filepath", label="Record your question")
|
158 |
+
],
|
159 |
+
outputs=[
|
160 |
+
gr.Text(label="Transcription"),
|
161 |
+
gr.Text(lines=10, label="Response"),
|
162 |
+
gr.Audio(label="Listen to Response") # New audio output for the synthesized speech
|
163 |
+
],
|
164 |
+
title="Personalized Nutrition AI Advisor",
|
165 |
+
description="Ask me anything about nutrition. Provide your Gender, Plan Type, Weight and Height for personalized advice."
|
166 |
+
)
|
167 |
|
168 |
interface2 = gr.Interface(
|
169 |
fn=emergency_assistance,
|
170 |
inputs=[gr.Textbox(lines=10, label="Query", placeholder="Enter your emergency nutrition query here...")],
|
171 |
outputs=[
|
172 |
gr.Text(lines=10, label="Response"),
|
173 |
+
gr.Audio(label="Listen to Response") # New audio output for the synthesized speech
|
174 |
],
|
175 |
title="Emergency Assistance",
|
176 |
description="To better assist you, could you explain what led to this emergency?"
|
|
|
178 |
|
179 |
|
180 |
# Combined interface with tabs
|
181 |
+
app = gr.TabbedInterface([interface1, interface2], ["Nutrition Consultant", "Emergency Assistance"], title="HealthyBytes: Your AI Nutrition Consultant")
|
182 |
|
183 |
if __name__ == "__main__":
|
184 |
+
app.launch(share=False)
|