arpit13 commited on
Commit
72f9a72
·
verified ·
1 Parent(s): 44a2862

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -9
app.py CHANGED
@@ -2,10 +2,13 @@
2
  import gradio as gr
3
  import openai
4
  import os
 
5
 
 
6
  openai.api_key= os.getenv("TRY_NEW_THINGS")
7
  openai.api_base = "https://api.groq.com/openai/v1"
8
 
 
9
  def get_groq_response(message):
10
  try:
11
  response = openai.ChatCompletion.create(
@@ -19,19 +22,36 @@ def get_groq_response(message):
19
  except Exception as e:
20
  return f"Error:{str(e)}"
21
 
22
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def chatbot(user_input,history=[]):
24
  bot_response = get_groq_response(user_input)
25
  history.append((user_input,bot_response))
26
  return history,history
27
 
28
- chat_interface = gr.Interface(
29
- fn = chatbot,
30
- inputs = ["text","state"],
31
- outputs = ["chatbot","state"],
32
- live = True,
33
- title ="Meet your Personal Assistant: Your Helpful and Caring Chatbot",
34
- description ="This chatbot is here to help you with anything you need, whether it’s answering questions, offering support, or guiding you through tasks. With a friendly and empathetic approach, it listens carefully to your concerns and provides thoughtful, understanding responses. Whether you’re seeking information or just someone to chat with, our chatbot is designed to make you feel heard and supported. It’s more than just a tool—it’s a companion dedicated to making your day easier and more enjoyable"
35
- )
36
 
 
 
 
 
 
 
 
 
 
 
37
  chat_interface.launch()
 
2
  import gradio as gr
3
  import openai
4
  import os
5
+ import speech_recognition as sr
6
 
7
+ # Set OpenAI API Key
8
  openai.api_key= os.getenv("TRY_NEW_THINGS")
9
  openai.api_base = "https://api.groq.com/openai/v1"
10
 
11
+ # Function to get response from GROQ API
12
  def get_groq_response(message):
13
  try:
14
  response = openai.ChatCompletion.create(
 
22
  except Exception as e:
23
  return f"Error:{str(e)}"
24
 
25
+ # Function to convert speech to text
26
+ def speech_to_text(audio):
27
+ # Initialize recognizer
28
+ recognizer = sr.Recognizer()
29
+
30
+ # Convert audio to text
31
+ try:
32
+ with sr.AudioFile(audio.name) as source:
33
+ audio_data = recognizer.record(source)
34
+ text = recognizer.recognize_google(audio_data)
35
+ return text
36
+ except Exception as e:
37
+ return f"Error in speech recognition: {str(e)}"
38
+
39
+
40
+ # Chatbot function
41
  def chatbot(user_input,history=[]):
42
  bot_response = get_groq_response(user_input)
43
  history.append((user_input,bot_response))
44
  return history,history
45
 
 
 
 
 
 
 
 
 
46
 
47
+
48
+ # Gradio Interface setup with speech-to-text functionality
49
+ chat_interface = gr.Interface(
50
+ fn=chatbot,
51
+ inputs=[gr.Audio(source="microphone", type="file"), "state"], # Added speech-to-text input
52
+ outputs=["chatbot", "state"],
53
+ live=True,
54
+ title="Meet your Personal Assistant: Your Helpful and Caring Chatbot",
55
+ description="This chatbot is here to help you with anything you need, whether it’s answering questions, offering support, or guiding you through tasks. With a friendly and empathetic approach, it listens carefully to your concerns and provides thoughtful, understanding responses. Whether you’re seeking information or just someone to chat with, our chatbot is designed to make you feel heard and supported. It’s more than just a tool—it’s a companion dedicated to making your day easier and more enjoyable.",
56
+ )
57
  chat_interface.launch()