awais0300 commited on
Commit
21763af
·
verified ·
1 Parent(s): 7559acc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -4,14 +4,14 @@ from gtts import gTTS
4
  import gradio as gr
5
  import speech_recognition as sr
6
 
7
- # Hardcode the API key for debugging (replace 'your-api-key' with your actual API key)
8
- API_KEY = "tgp_v1_YpR1d9dFBA6bLBE3L5dKYMj53VQ1LFI9ag9SqeV9Cmk" # Replace with your actual API key
9
 
10
  # Function to get chatbot response using the Together API
11
  def get_chatbot_response(user_input):
12
  url = "https://api.together.ai/v1/chat/completions" # Ensure this URL is correct
13
  headers = {
14
- "Authorization": f"Bearer {API_KEY}",
15
  "Content-Type": "application/json",
16
  }
17
  data = {
@@ -23,20 +23,23 @@ def get_chatbot_response(user_input):
23
  response = requests.post(url, json=data, headers=headers)
24
 
25
  # Debugging: print the status code and response content
26
- print(f"Status Code: {response.status_code}")
27
  try:
28
- print(f"API Response: {response.json()}")
29
- except Exception as e:
30
- print(f"Error parsing response: {e}")
31
-
 
 
32
  # Check if the API call was successful
33
  if response.status_code == 200:
34
- response_data = response.json()
35
  if 'choices' in response_data:
36
  return response_data['choices'][0]['message']['content']
37
  else:
 
38
  return "Error: No valid response found."
39
  else:
 
40
  return f"Error: API request failed with status code {response.status_code}"
41
 
42
  # Text-to-Speech (TTS) using gTTS
 
4
  import gradio as gr
5
  import speech_recognition as sr
6
 
7
+ # Set your API key for Together API
8
+ os.environ["TOGETHER_API_KEY"] = "tgp_v1_YpR1d9dFBA6bLBE3L5dKYMj53VQ1LFI9ag9SqeV9Cmk" # Replace with your actual API key
9
 
10
  # Function to get chatbot response using the Together API
11
  def get_chatbot_response(user_input):
12
  url = "https://api.together.ai/v1/chat/completions" # Ensure this URL is correct
13
  headers = {
14
+ "Authorization": f"Bearer {os.environ['TOGETHER_API_KEY']}", # Ensure your API key is correct
15
  "Content-Type": "application/json",
16
  }
17
  data = {
 
23
  response = requests.post(url, json=data, headers=headers)
24
 
25
  # Debugging: print the status code and response content
26
+ print(f"Status Code: {response.status_code}") # Debugging status code
27
  try:
28
+ response_data = response.json() # Try to parse the response
29
+ print(f"API Response: {response_data}") # Debugging the API response
30
+ except ValueError:
31
+ print("Error: Unable to parse JSON response.")
32
+ return "Error: Unable to parse the response from the API."
33
+
34
  # Check if the API call was successful
35
  if response.status_code == 200:
 
36
  if 'choices' in response_data:
37
  return response_data['choices'][0]['message']['content']
38
  else:
39
+ print("Error: No valid 'choices' found in response.")
40
  return "Error: No valid response found."
41
  else:
42
+ print(f"Error: API request failed with status code {response.status_code}.")
43
  return f"Error: API request failed with status code {response.status_code}"
44
 
45
  # Text-to-Speech (TTS) using gTTS