kathirog commited on
Commit
ade854d
·
verified ·
1 Parent(s): 3bf0001

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -31
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 = "AIzaSyD7Xm52Snz28g5Ddpd3d4SXgxLWE0K7Ong" # Replace with your actual API key
10
- MODEL_NAME = "mistralai/mistral-7b-instruct" # Ensure this model is accessible
11
 
12
  # Check if API key is set
13
  if not API_KEY:
14
- raise ValueError("Error: Hugging Face API key is missing. Set HF_API_KEY as an environment variable.")
15
 
16
- # Verify API Key
17
- headers = {"Authorization": f"Bearer {API_KEY}"}
18
- response = requests.get("https://huggingface.co/api/whoami-v2", headers=headers)
 
 
19
 
20
- if response.status_code != 200:
21
- raise ValueError(f"Invalid API Key! Error: {response.json()}")
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
- # Log input messages for debugging
56
- print("Sending messages:", messages)
57
-
58
- # Get response from Hugging Face API using the correct method
59
- chat_response = client.chat(messages=messages)
60
 
61
- # Log API response for debugging
62
- print("Raw API Response:", chat_response)
63
 
64
  # Validate API Response
65
- if chat_response is None or "error" in chat_response:
66
- return "Error: No response from API. Check API key and model permissions.", None
67
 
68
  # Extract chatbot response
69
- response = chat_response.get("choices", [{}])[0].get("message", {}).get("content", "").strip()
70
 
71
- if not response:
72
- response = "Error: Model returned an empty response."
 
 
73
 
74
  except Exception as e:
75
- response = f"Error: {str(e)}"
76
  print("Exception Occurred:", e)
77
 
78
  # Convert response to speech
79
- audio_output = text_to_voice(response)
80
- return response, audio_output
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