dschandra commited on
Commit
92f2b60
·
verified ·
1 Parent(s): 749d0c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -51
app.py CHANGED
@@ -1,68 +1,58 @@
1
- import gradio as gr
2
  from gtts import gTTS
3
- import openai
4
- import speech_recognition as sr
5
  import os
6
 
7
- # Set OpenAI API Key
8
- openai.api_key = "YOUR_OPENAI_API_KEY" # Replace with your OpenAI API Key
9
-
10
- # Text-to-Speech Function
11
- def text_to_speech(response_text):
12
- tts = gTTS(response_text, lang="en")
 
 
 
 
 
 
 
 
 
13
  audio_file = "response.mp3"
14
  tts.save(audio_file)
15
  return audio_file
16
 
17
- # Speech Recognition Function
18
- def speech_to_text(audio_file):
19
- recognizer = sr.Recognizer()
20
- with sr.AudioFile(audio_file) as source:
21
- audio_data = recognizer.record(source)
22
- try:
23
- return recognizer.recognize_google(audio_data)
24
- except sr.UnknownValueError:
25
- return "I'm sorry, I couldn't understand that. Could you repeat?"
26
- except sr.RequestError:
27
- return "There was an error with the speech recognition service."
28
-
29
- # Chatbot Logic using OpenAI GPT
30
- def chatbot_response(user_input):
31
  try:
32
- response = openai.Completion.create(
33
- engine="text-davinci-003", # Use a powerful GPT model
34
- prompt=f"User: {user_input}\nChatbot:",
35
- max_tokens=150,
36
- temperature=0.7,
37
- )
38
- return response.choices[0].text.strip()
39
  except Exception as e:
40
- return f"Error generating response: {e}"
41
-
42
- # Gradio Interface Logic
43
- def process_interaction(audio_file):
44
- # Convert user speech to text
45
- user_text = speech_to_text(audio_file)
46
- if "Error" in user_text or "sorry" in user_text:
47
- return user_text, None
48
 
49
- # Get chatbot response
50
- chatbot_reply = chatbot_response(user_text)
 
 
 
 
 
 
 
51
 
52
- # Convert chatbot reply to speech
53
- chatbot_audio = text_to_speech(chatbot_reply)
54
 
55
- return chatbot_reply, chatbot_audio
56
 
57
  # Gradio Interface
58
- interface = gr.Interface(
59
- fn=process_interaction,
60
- inputs=gr.Audio(source="microphone", type="filepath"),
61
- outputs=[gr.Textbox(label="Chatbot Reply"), gr.Audio(label="Chatbot Voice Reply")],
62
- title="Face-to-Face Chatbot",
63
- description="Talk to this chatbot like you're having a real conversation! Speak into your microphone to start.",
64
- live=True,
65
  )
66
 
67
  if __name__ == "__main__":
68
- interface.launch()
 
1
+ from transformers import pipeline
2
  from gtts import gTTS
3
+ import gradio as gr
 
4
  import os
5
 
6
+ # Initialize Whisper pipeline for speech-to-text
7
+ pipe = pipeline("automatic-speech-recognition", model="openai/whisper-large-v3-turbo")
8
+
9
+ # Menu for the restaurant
10
+ menu = {
11
+ "Starters": ["Soup", "Spring Rolls"],
12
+ "Main Course": ["Paneer Butter Masala", "Chicken Curry", "Veg Biryani"],
13
+ "Breads": ["Roti", "Naan", "Paratha"],
14
+ "Desserts": ["Gulab Jamun", "Ice Cream"],
15
+ "Drinks": ["Mango Lassi", "Soda", "Water"]
16
+ }
17
+
18
+ # Function to convert text to speech
19
+ def text_to_speech(text):
20
+ tts = gTTS(text, lang="en")
21
  audio_file = "response.mp3"
22
  tts.save(audio_file)
23
  return audio_file
24
 
25
+ # Chatbot logic
26
+ def chatbot_conversation(audio_file):
27
+ # Speech-to-text using Whisper
 
 
 
 
 
 
 
 
 
 
 
28
  try:
29
+ transcription = pipe(audio_file)["text"]
 
 
 
 
 
 
30
  except Exception as e:
31
+ return f"Error: {e}", None
 
 
 
 
 
 
 
32
 
33
+ # Generate a response based on transcription
34
+ if "menu" in transcription.lower():
35
+ response = "Our menu categories are: " + ", ".join(menu.keys())
36
+ elif "order" in transcription.lower():
37
+ response = "What would you like to order? We have " + ", ".join(menu["Main Course"])
38
+ elif "thank you" in transcription.lower():
39
+ response = "You're welcome! Enjoy your meal!"
40
+ else:
41
+ response = "I'm sorry, I didn't understand that. Could you please repeat?"
42
 
43
+ # Convert response to audio
44
+ audio_response = text_to_speech(response)
45
 
46
+ return response, audio_response
47
 
48
  # Gradio Interface
49
+ iface = gr.Interface(
50
+ fn=chatbot_conversation,
51
+ inputs=gr.Audio(type="filepath"),
52
+ outputs=[gr.Textbox(label="Transcription"), gr.Audio(label="Response Audio")],
53
+ title="Restaurant Chatbot with Whisper ASR",
54
+ description="Speak to the chatbot and get a response!",
 
55
  )
56
 
57
  if __name__ == "__main__":
58
+ iface.launch()