Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 7 |
-
|
| 8 |
-
SYSTEM_PROMPT = "You're a helpful cooking assistant. Provide detailed recipes."
|
| 9 |
|
| 10 |
-
client = InferenceClient(
|
| 11 |
|
| 12 |
-
def
|
| 13 |
"""Secure API endpoint"""
|
| 14 |
if api_key != API_KEY:
|
| 15 |
return {"error": "Invalid API key"}
|
| 16 |
|
| 17 |
-
|
| 18 |
-
{"role": "
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return client.chat_completion(messages, max_tokens=500)
|
| 24 |
-
except Exception as e:
|
| 25 |
-
return {"error": str(e)}
|
| 26 |
|
| 27 |
-
# Minimal UI
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 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)
|