jsakshi commited on
Commit
55df7ee
·
verified ·
1 Parent(s): 21f7458

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -26
app.py CHANGED
@@ -2,33 +2,37 @@ import requests
2
  import os
3
  import gradio as gr
4
 
5
- API_TOKEN = os.getenv("HF_API_TOKEN")
6
- if not API_TOKEN:
7
- raise ValueError("HF_API_TOKEN not found. Please set it in the Space Secrets.")
8
 
9
- MODEL_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"
 
10
 
11
  HEADERS = {
12
- "Authorization": f"Bearer {API_TOKEN}",
13
- "Content-Type": "application/json"
 
 
14
  }
15
 
16
- def query_deepseek(prompt):
17
- """Send a prompt to DeepSeek-R1 and get a response."""
18
  payload = {
19
- "inputs": prompt,
20
- "parameters": {
21
- "max_new_tokens": 100,
22
- "temperature": 0.6,
23
- "top_p": 0.9
24
- }
 
25
  }
26
 
27
  try:
28
  response = requests.post(MODEL_URL, headers=HEADERS, json=payload)
29
- response.raise_for_status()
30
  result = response.json()
31
- return result[0]["generated_text"].strip()
32
  except requests.exceptions.RequestException as e:
33
  return f"Error: Could not connect to the API - {str(e)}"
34
  except (KeyError, IndexError):
@@ -36,33 +40,28 @@ def query_deepseek(prompt):
36
 
37
  def chat_interface(user_input, history):
38
  """Handle chatbot interaction with Gradio."""
39
- prompt = f"User: {user_input}\nAssistant: "
40
- response = query_deepseek(prompt)
41
- return response
42
 
43
  css = """
44
  button {
45
- background-color: #007bff !important; /* Blue background */
46
- color: white !important; /* White text */
47
  border: none !important;
48
  padding: 10px 20px !important;
49
  border-radius: 5px !important;
50
  }
51
  button:hover {
52
- background-color: #0056b3 !important; /* Darker blue on hover */
53
  }
54
  """
55
 
56
-
57
  interface = gr.ChatInterface(
58
  fn=chat_interface,
59
  title="LocoBot : Your AI Companion",
60
- description="Engage in smart, dynamic conversations with LocoBot",
61
  theme="default",
62
  css=css
63
  )
64
 
65
  if __name__ == "__main__":
66
  interface.launch(server_name="0.0.0.0", server_port=7860)
67
-
68
-
 
2
  import os
3
  import gradio as gr
4
 
5
+ API_KEY = os.getenv("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")
6
+ if not API_KEY:
7
+ raise ValueError("OPENROUTER_API_KEY not found. Please set it in your environment variables.")
8
 
9
+ # OpenRouter API endpoint
10
+ MODEL_URL = "https://openrouter.ai/api/v1/chat/completions"
11
 
12
  HEADERS = {
13
+ "Authorization": f"Bearer {API_KEY}",
14
+ "Content-Type": "application/json",
15
+ "HTTP-Referer": "https://your-app-url.com", # Optional but recommended
16
+ "X-Title": "LocoBot" # Optional: your app name
17
  }
18
 
19
+ def query_gpt35(prompt):
20
+ """Send a prompt to GPT-3.5-Turbo via OpenRouter and get a response."""
21
  payload = {
22
+ "model": "openai/gpt-3.5-turbo",
23
+ "messages": [
24
+ {"role": "system", "content": "You are LocoBot, a helpful assistant."},
25
+ {"role": "user", "content": prompt}
26
+ ],
27
+ "max_tokens": 200,
28
+ "temperature": 0.7
29
  }
30
 
31
  try:
32
  response = requests.post(MODEL_URL, headers=HEADERS, json=payload)
33
+ response.raise_for_status()
34
  result = response.json()
35
+ return result["choices"][0]["message"]["content"].strip()
36
  except requests.exceptions.RequestException as e:
37
  return f"Error: Could not connect to the API - {str(e)}"
38
  except (KeyError, IndexError):
 
40
 
41
  def chat_interface(user_input, history):
42
  """Handle chatbot interaction with Gradio."""
43
+ return query_gpt35(user_input)
 
 
44
 
45
  css = """
46
  button {
47
+ background-color: #007bff !important;
48
+ color: white !important;
49
  border: none !important;
50
  padding: 10px 20px !important;
51
  border-radius: 5px !important;
52
  }
53
  button:hover {
54
+ background-color: #0056b3 !important;
55
  }
56
  """
57
 
 
58
  interface = gr.ChatInterface(
59
  fn=chat_interface,
60
  title="LocoBot : Your AI Companion",
61
+ description="Engage in smart, dynamic conversations with LocoBot (powered by GPT-3.5 Turbo via OpenRouter)",
62
  theme="default",
63
  css=css
64
  )
65
 
66
  if __name__ == "__main__":
67
  interface.launch(server_name="0.0.0.0", server_port=7860)