Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,24 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
import speech_recognition as sr
|
| 4 |
import pyttsx3
|
| 5 |
-
import os
|
| 6 |
import requests
|
| 7 |
|
| 8 |
-
# Load API Key securely
|
| 9 |
-
API_KEY = "
|
| 10 |
-
MODEL_NAME = "
|
| 11 |
|
| 12 |
# Check if API key is set
|
| 13 |
if not API_KEY:
|
| 14 |
-
raise ValueError("Error:
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
headers = {
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
print("API Key is valid!")
|
| 24 |
-
|
| 25 |
-
# Initialize the InferenceClient
|
| 26 |
-
client = InferenceClient(model=MODEL_NAME, token=API_KEY)
|
| 27 |
|
| 28 |
# System message for the chatbot
|
| 29 |
system_message = "You are a friendly and helpful chatbot."
|
|
@@ -52,32 +47,34 @@ def respond(message, history=None, audio_input=None):
|
|
| 52 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 53 |
messages.append({"role": "user", "content": message})
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
|
| 64 |
# Validate API Response
|
| 65 |
-
if
|
| 66 |
-
return "Error: No response from API. Check API key and model permissions.", None
|
| 67 |
|
| 68 |
# Extract chatbot response
|
| 69 |
-
|
| 70 |
|
| 71 |
-
if
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
-
|
| 76 |
print("Exception Occurred:", e)
|
| 77 |
|
| 78 |
# Convert response to speech
|
| 79 |
-
audio_output = text_to_voice(
|
| 80 |
-
return
|
| 81 |
|
| 82 |
|
| 83 |
# Convert audio to text
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import speech_recognition as sr
|
| 3 |
import pyttsx3
|
|
|
|
| 4 |
import requests
|
| 5 |
|
| 6 |
+
# Load Groq API Key securely
|
| 7 |
+
API_KEY = "your_groq_api_key_here" # Replace with your actual Groq API key
|
| 8 |
+
MODEL_NAME = "groq-model-id" # Ensure this model is accessible on Groq Cloud
|
| 9 |
|
| 10 |
# Check if API key is set
|
| 11 |
if not API_KEY:
|
| 12 |
+
raise ValueError("Error: Groq API key is missing. Set the API key.")
|
| 13 |
|
| 14 |
+
# Headers for authorization
|
| 15 |
+
headers = {
|
| 16 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 17 |
+
"Content-Type": "application/json"
|
| 18 |
+
}
|
| 19 |
|
| 20 |
+
# URL for Groq Cloud model endpoint (example URL, replace with actual Groq Cloud endpoint)
|
| 21 |
+
url = f"https://api.groq.ai/v1/models/{MODEL_NAME}/predict"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# System message for the chatbot
|
| 24 |
system_message = "You are a friendly and helpful chatbot."
|
|
|
|
| 47 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 48 |
messages.append({"role": "user", "content": message})
|
| 49 |
|
| 50 |
+
# Prepare the payload for Groq Cloud
|
| 51 |
+
payload = {
|
| 52 |
+
"inputs": message,
|
| 53 |
+
"parameters": {"max_new_tokens": 512} # Optional parameters like max tokens
|
| 54 |
+
}
|
| 55 |
|
| 56 |
+
# Send the request to Groq Cloud API
|
| 57 |
+
response = requests.post(url, json=payload, headers=headers)
|
| 58 |
|
| 59 |
# Validate API Response
|
| 60 |
+
if response.status_code != 200:
|
| 61 |
+
return f"Error: No response from API. Check API key and model permissions. Status: {response.status_code}", None
|
| 62 |
|
| 63 |
# Extract chatbot response
|
| 64 |
+
chat_response = response.json()
|
| 65 |
|
| 66 |
+
if "generated_text" in chat_response:
|
| 67 |
+
response_text = chat_response["generated_text"].strip()
|
| 68 |
+
else:
|
| 69 |
+
response_text = "Error: Model returned an empty response."
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
+
response_text = f"Error: {str(e)}"
|
| 73 |
print("Exception Occurred:", e)
|
| 74 |
|
| 75 |
# Convert response to speech
|
| 76 |
+
audio_output = text_to_voice(response_text)
|
| 77 |
+
return response_text, audio_output
|
| 78 |
|
| 79 |
|
| 80 |
# Convert audio to text
|