luminoussg commited on
Commit
518be16
·
verified ·
1 Parent(s): d8a9934

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -17
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import os
2
  import gradio as gr
3
- import openai
4
 
5
- # We'll read the Hugging Face API key from environment variables (using Spaces "secrets").
6
  HF_API_KEY = os.getenv("HF_API_KEY")
7
 
8
  # Model endpoints on Hugging Face
@@ -14,20 +14,32 @@ MODEL_ENDPOINTS = {
14
 
15
  def query_model(prompt, model_endpoint):
16
  """
17
- Query a specific model using OpenAI-compatible ChatCompletion.
18
- Since the Hugging Face Inference API is OpenAI-compatible,
19
- we set openai.api_base to the model's endpoint.
20
  """
21
- openai.api_key = HF_API_KEY
22
- openai.api_base = model_endpoint
23
-
24
- response = openai.ChatCompletion.create(
25
- model="placeholder-model",
26
- messages=[{"role": "user", "content": prompt}],
27
- max_tokens=512,
28
- temperature=0.7,
29
- )
30
- return response.choices[0].message["content"]
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  def chat_with_models(user_input, history):
33
  responses = []
@@ -39,8 +51,7 @@ def chat_with_models(user_input, history):
39
  return history, history
40
 
41
  with gr.Blocks() as demo:
42
- gr.Markdown("# Multi-LLM Chatbot using Hugging Face Inference API (OpenAI-compatible)")
43
-
44
  chatbot = gr.Chatbot()
45
  msg = gr.Textbox(label="Your Message")
46
  clear = gr.Button("Clear")
 
1
  import os
2
  import gradio as gr
3
+ import requests
4
 
5
+ # Get the Hugging Face API key from Spaces secrets.
6
  HF_API_KEY = os.getenv("HF_API_KEY")
7
 
8
  # Model endpoints on Hugging Face
 
14
 
15
  def query_model(prompt, model_endpoint):
16
  """
17
+ Query a model via Hugging Face Inference API using a requests.post call.
18
+ This assumes an OpenAI-compatible endpoint structure.
 
19
  """
20
+ headers = {
21
+ "Authorization": f"Bearer {HF_API_KEY}",
22
+ "Content-Type": "application/json"
23
+ }
24
+ data = {
25
+ "messages": [{"role": "user", "content": prompt}],
26
+ "max_tokens": 512,
27
+ "temperature": 0.7
28
+ }
29
+
30
+ response = requests.post(model_endpoint, headers=headers, json=data)
31
+ try:
32
+ result = response.json()
33
+ except Exception:
34
+ return f"Error: Unable to parse JSON. Response: {response.text}"
35
+
36
+ if "error" in result:
37
+ return f"Error: {result['error']}"
38
+
39
+ try:
40
+ return result["choices"][0]["message"]["content"]
41
+ except Exception:
42
+ return f"Error: Unexpected response format: {result}"
43
 
44
  def chat_with_models(user_input, history):
45
  responses = []
 
51
  return history, history
52
 
53
  with gr.Blocks() as demo:
54
+ gr.Markdown("# Multi-LLM Chatbot using Hugging Face Inference API")
 
55
  chatbot = gr.Chatbot()
56
  msg = gr.Textbox(label="Your Message")
57
  clear = gr.Button("Clear")