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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -23
app.py CHANGED
@@ -3,34 +3,42 @@ from huggingface_hub import InferenceClient
3
  import os
4
 
5
  # Configuration
6
- API_KEY = os.getenv("API_KEY", "your-secret-key") # Set in Space Secrets
7
- MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
8
- SYSTEM_PROMPT = "You're a helpful cooking assistant. Provide detailed recipes."
9
 
10
- client = InferenceClient(MODEL_NAME)
11
 
12
- def generate_response(message: str, api_key: str):
13
  """Secure API endpoint"""
14
  if api_key != API_KEY:
15
  return {"error": "Invalid API key"}
16
 
17
- messages = [
18
- {"role": "system", "content": SYSTEM_PROMPT},
19
- {"role": "user", "content": message}
20
- ]
21
-
22
- try:
23
- return client.chat_completion(messages, max_tokens=500)
24
- except Exception as e:
25
- return {"error": str(e)}
26
 
27
- # Minimal UI for testing
28
- demo = gr.Interface(
29
- fn=lambda msg: generate_response(msg, API_KEY),
30
- inputs="text",
31
- outputs="json",
32
- title="🍳 Recipe API Gateway",
33
- description=f"API Key: `{API_KEY}`"
34
- )
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- demo.launch()
 
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)