kathirog commited on
Commit
5650516
·
verified ·
1 Parent(s): 56fa0a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -65
app.py CHANGED
@@ -1,76 +1,42 @@
 
1
  import gradio as gr
2
- import requests
3
- import speech_recognition as sr
4
  import pyttsx3
 
5
 
6
- # Define the Groq Cloud API key and model name
7
- API_KEY = "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" # Replace with your Groq Cloud API Key
8
- MODEL_NAME = "groq-model-id" # Replace with the actual model ID from Groq Cloud
9
 
10
- # Groq Cloud API endpoint
11
- API_URL = f"https://api.groq.ai/v1/models/{MODEL_NAME}/predict"
12
 
13
- # Verify API Key with Groq Cloud
14
  headers = {"Authorization": f"Bearer {API_KEY}"}
15
- response = requests.get("https://api.groq.ai/v1/whoami", headers=headers)
16
-
17
- # Check if API Key is valid
18
- if response.status_code != 200:
19
- raise ValueError(f"Invalid API Key! Error: {response.json()}")
20
- else:
21
- print("API Key is valid!")
22
-
23
- # Function to process the chat input
24
- def respond(message, history=None, audio_input=None):
25
- if history is None:
26
- history = []
27
 
28
- # If audio input is provided, convert it to text
29
- if audio_input:
30
- message = voice_to_text(audio_input)
31
-
32
- # Prepare message history for context
33
- messages = [{"role": "system", "content": "You are a friendly chatbot."}]
34
- for user_msg, bot_msg in history:
35
- if user_msg:
36
- messages.append({"role": "user", "content": user_msg})
37
- if bot_msg:
38
- messages.append({"role": "assistant", "content": bot_msg})
39
- messages.append({"role": "user", "content": message})
40
-
41
- # Send message to Groq Cloud API
42
  try:
43
- # API request payload
44
- payload = {
45
- "inputs": message,
46
- "parameters": {"max_new_tokens": 512}
47
- }
48
-
49
- # Send POST request to the Groq Cloud API
50
- response = requests.post(API_URL, headers=headers, json=payload)
51
-
52
- if response.status_code == 200:
53
- # Get the chatbot's response from the API
54
- chat_response = response.json()
55
- chatbot_reply = chat_response.get("generated_text", "")
56
- else:
57
- raise ValueError(f"API Error: {response.status_code}, {response.text}")
58
-
59
- except Exception as e:
60
- chatbot_reply = f"Error: {str(e)}"
61
-
62
- # Convert text response to speech
63
- audio_output = text_to_voice(chatbot_reply)
64
- return chatbot_reply, audio_output
65
-
66
-
67
- # Convert voice input (audio) to text
68
  def voice_to_text(audio_path):
69
  recognizer = sr.Recognizer()
70
  try:
71
  with sr.AudioFile(audio_path) as source:
72
  audio_data = recognizer.record(source)
73
- text = recognizer.recognize_google(audio_data) # Using Google's speech recognition
74
  except sr.UnknownValueError:
75
  text = "Sorry, I could not understand the audio."
76
  except sr.RequestError:
@@ -79,8 +45,7 @@ def voice_to_text(audio_path):
79
  text = f"Audio Processing Error: {str(e)}"
80
  return text
81
 
82
-
83
- # Convert text to speech (voice output)
84
  def text_to_voice(text):
85
  try:
86
  audio_filename = "response.mp3"
@@ -89,16 +54,37 @@ def text_to_voice(text):
89
  engine.runAndWait()
90
  return audio_filename
91
  except Exception as e:
92
- print(f"Error converting text to speech: {e}")
93
  return None
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
- # Create the Gradio interface
97
  demo = gr.Interface(
98
  fn=respond,
99
  inputs=[
100
  gr.Textbox(label="Text Input", placeholder="Enter your message..."),
101
- gr.Audio(type="filepath", label="Audio Input"),
102
  ],
103
  outputs=[
104
  gr.Textbox(label="Chatbot Response"),
 
1
+ \import requests
2
  import gradio as gr
 
 
3
  import pyttsx3
4
+ import speech_recognition as sr
5
 
6
+ # Replace with your actual API key
7
+ API_KEY = "AIzaSyAm0RSpUKY38494Fug8SPIpdHLXE2d3cps" # Replace with your Gemini 2.0 Flash API key
8
+ MODEL_NAME = "gemini-2.0-flash" # Replace with your Gemini model name, e.g., "Gemini 2.0 flash"
9
 
10
+ # API URL for Gemini 2.0 Flash
11
+ API_URL = "https://api.gemini.com/v1/predict" # Example URL; make sure to replace with actual endpoint if different
12
 
13
+ # Headers to pass the API Key
14
  headers = {"Authorization": f"Bearer {API_KEY}"}
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ # Function to make the API call to Gemini
17
+ def gemini_api_request(message):
 
 
 
 
 
 
 
 
 
 
 
 
18
  try:
19
+ response = requests.post(
20
+ API_URL,
21
+ headers=headers,
22
+ json={"input": message}
23
+ )
24
+
25
+ # Check if the response status code is OK
26
+ response.raise_for_status()
27
+ result = response.json()
28
+ return result.get("output", "Error: No output returned from API.")
29
+
30
+ except requests.exceptions.RequestException as e:
31
+ return f"Error: {str(e)}"
32
+
33
+ # Convert audio to text
 
 
 
 
 
 
 
 
 
 
34
  def voice_to_text(audio_path):
35
  recognizer = sr.Recognizer()
36
  try:
37
  with sr.AudioFile(audio_path) as source:
38
  audio_data = recognizer.record(source)
39
+ text = recognizer.recognize_google(audio_data)
40
  except sr.UnknownValueError:
41
  text = "Sorry, I could not understand the audio."
42
  except sr.RequestError:
 
45
  text = f"Audio Processing Error: {str(e)}"
46
  return text
47
 
48
+ # Convert text to speech
 
49
  def text_to_voice(text):
50
  try:
51
  audio_filename = "response.mp3"
 
54
  engine.runAndWait()
55
  return audio_filename
56
  except Exception as e:
57
+ print(f"TTS Error: {e}")
58
  return None
59
 
60
+ # Function to handle both text and voice input/output
61
+ def respond(message, history=None, audio_input=None):
62
+ try:
63
+ if history is None:
64
+ history = []
65
+
66
+ if audio_input:
67
+ message = voice_to_text(audio_input)
68
+
69
+ if not message.strip():
70
+ return "Error: No input provided.", None
71
+
72
+ # Make request to Gemini API for processing
73
+ response = gemini_api_request(message)
74
+
75
+ # Convert response to audio
76
+ audio_output = text_to_voice(response)
77
+
78
+ return response, audio_output
79
+ except Exception as e:
80
+ return f"Error: {str(e)}", None
81
 
82
+ # Gradio UI
83
  demo = gr.Interface(
84
  fn=respond,
85
  inputs=[
86
  gr.Textbox(label="Text Input", placeholder="Enter your message..."),
87
+ gr.Audio(type="filepath", label="Audio Input")
88
  ],
89
  outputs=[
90
  gr.Textbox(label="Chatbot Response"),