sarthak413 commited on
Commit
d09f15e
Β·
verified Β·
1 Parent(s): 15673c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -29
app.py CHANGED
@@ -2,43 +2,29 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
- # Configuration
6
- API_KEY = os.getenv("API_KEY", "your-secret-key-123") # Set in Settings β†’ Secrets
7
- MODEL = "HuggingFaceH4/zephyr-7b-beta"
8
 
9
- client = InferenceClient(MODEL)
10
-
11
- def chat_api(message: str, api_key: str):
12
- """Secure API endpoint"""
13
  if api_key != API_KEY:
14
  return {"error": "Invalid API key"}
15
 
16
  response = client.chat_completion(
17
  messages=[{"role": "user", "content": message}],
18
- max_tokens=500,
19
- stream=False
20
  )
21
  return {"response": response.choices[0].message.content}
22
 
23
- # Minimal UI with API docs
24
  with gr.Blocks() as demo:
25
- gr.Markdown("## 🍳 Recipe Chatbot API Gateway")
26
- with gr.Tab("Chat"):
27
- gr.ChatInterface(lambda msg: chat_api(msg, API_KEY)["response"])
28
- with gr.Tab("API Docs"):
29
- gr.Markdown(f"""
30
- ### API Endpoint
31
- ```
32
- POST https://sarthak413-chatbot.hf.space/api/predict
33
- ```
34
- **Required JSON:**
35
- ```json
36
- {{
37
- "data": [
38
- "your message",
39
- "your-api-key-here"
40
- ]
41
- }}
42
- """)
43
 
44
- demo.launch(show_api=True)
 
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
+ API_KEY = os.getenv("API_KEY", "your-secret-key-123") # Set in Space Settings
6
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
7
 
8
+ def predict(message, api_key):
 
 
 
9
  if api_key != API_KEY:
10
  return {"error": "Invalid API key"}
11
 
12
  response = client.chat_completion(
13
  messages=[{"role": "user", "content": message}],
14
+ max_tokens=500
 
15
  )
16
  return {"response": response.choices[0].message.content}
17
 
18
+ # Create Gradio interface
19
  with gr.Blocks() as demo:
20
+ with gr.Tab("API Endpoint"):
21
+ gr.Interface(
22
+ fn=predict,
23
+ inputs=[gr.Textbox(), gr.Textbox(type="password")],
24
+ outputs="json",
25
+ api_name="predict" # This creates the /run/predict endpoint
26
+ )
27
+ with gr.Tab("Chat UI"):
28
+ gr.ChatInterface(lambda msg: predict(msg, API_KEY)["response"])
 
 
 
 
 
 
 
 
 
29
 
30
+ demo.launch()