adeelshuaib commited on
Commit
e3851ba
·
verified ·
1 Parent(s): 920e657

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -25
app.py CHANGED
@@ -2,11 +2,28 @@ import gradio as gr
2
  from groq import Groq
3
  import os
4
  import time
 
 
5
 
6
  api_key = os.getenv('GROQ_API_KEY')
7
  # Initialize Groq client
8
  client = Groq(api_key=api_key)
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  # Function to generate responses with error handling
11
  def generate_response(user_input, chat_history: list):
12
  try:
@@ -26,7 +43,6 @@ def generate_response(user_input, chat_history: list):
26
  # Call Groq API to get a response from LLaMA
27
  chat_completion = client.chat.completions.create(
28
  messages=messages,
29
- #model="llama3-8b-8192"
30
  model='llama-3.1-70b-versatile'
31
  )
32
 
@@ -47,34 +63,27 @@ def gradio_interface():
47
  # Create input textbox and button for clearing chat
48
  gr.Markdown("## LoserHero - A Mental Health Chatbot")
49
  chatbot = gr.Chatbot(type="messages")
 
50
  msg = gr.Textbox(placeholder="Type your message here...")
 
 
51
  clear = gr.Button("Clear")
52
 
53
- # User message submission function
54
- def user(user_message, history: list):
55
- # Add user message to the history
56
- history.append({"role": "user", "content": user_message})
57
- return "", history # Reset message input and return updated history
58
-
59
- # Bot response function with simulated typing effect
60
- def bot(history: list):
61
- # Ensure that history is not empty
62
- if len(history) > 0:
63
- user_input = history[-1]["content"] # Get the last user message
64
- response, updated_history = generate_response(user_input, history) # Get bot's response
65
-
66
- history = updated_history # Update the history with the new response
67
- history.append({"role": "assistant", "content": ""}) # Add placeholder for assistant
68
-
69
- # Simulate typing effect for the bot's response
70
- for character in response:
71
- history[-1]['content'] += character
72
- time.sleep(0.02) # Typing delay
73
- yield history # Yield updated history as the bot types
74
 
75
- # Set up interaction flow:
76
- msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, chatbot, chatbot)
77
- clear.click(lambda: [], None, chatbot, queue=False) # Clear chat history when clicked
78
 
79
  demo.launch()
80
 
 
2
  from groq import Groq
3
  import os
4
  import time
5
+ import speech_recognition as sr
6
+ from gtts import gTTS
7
 
8
  api_key = os.getenv('GROQ_API_KEY')
9
  # Initialize Groq client
10
  client = Groq(api_key=api_key)
11
 
12
+ # Function to convert audio to text
13
+ def audio_to_text(audio_file):
14
+ recognizer = sr.Recognizer()
15
+ with sr.AudioFile(audio_file) as source:
16
+ audio_data = recognizer.record(source)
17
+ text = recognizer.recognize_google(audio_data)
18
+ return text
19
+
20
+ # Function to convert text to audio
21
+ def text_to_audio(text):
22
+ tts = gTTS(text)
23
+ audio_file = "output_audio.mp3"
24
+ tts.save(audio_file)
25
+ return audio_file
26
+
27
  # Function to generate responses with error handling
28
  def generate_response(user_input, chat_history: list):
29
  try:
 
43
  # Call Groq API to get a response from LLaMA
44
  chat_completion = client.chat.completions.create(
45
  messages=messages,
 
46
  model='llama-3.1-70b-versatile'
47
  )
48
 
 
63
  # Create input textbox and button for clearing chat
64
  gr.Markdown("## LoserHero - A Mental Health Chatbot")
65
  chatbot = gr.Chatbot(type="messages")
66
+ audio_input = gr.Audio(source="microphone", type="filepath", label="Speak your message")
67
  msg = gr.Textbox(placeholder="Type your message here...")
68
+ text_output = gr.Textbox(label="Response (Text)")
69
+ audio_output = gr.Audio(label="Response (Audio)")
70
  clear = gr.Button("Clear")
71
 
72
+ def process_input(user_message, audio_file, history: list):
73
+ if audio_file:
74
+ user_message = audio_to_text(audio_file)
75
+ if user_message:
76
+ history.append({"role": "user", "content": user_message})
77
+ response, updated_history = generate_response(user_message, history)
78
+ history = updated_history
79
+ history.append({"role": "assistant", "content": response})
80
+ response_audio = text_to_audio(response)
81
+ return "", None, response, response_audio, history
82
+ return "", None, "Please provide a valid input.", None, history
 
 
 
 
 
 
 
 
 
 
83
 
84
+ msg.submit(process_input, [msg, audio_input, chatbot], [msg, audio_input, text_output, audio_output, chatbot])
85
+ audio_input.change(process_input, [msg, audio_input, chatbot], [msg, audio_input, text_output, audio_output, chatbot])
86
+ clear.click(lambda: ("", None, "", None, []), None, [msg, audio_input, text_output, audio_output, chatbot])
87
 
88
  demo.launch()
89