Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,43 +2,29 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
MODEL = "HuggingFaceH4/zephyr-7b-beta"
|
| 8 |
|
| 9 |
-
|
| 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 |
-
#
|
| 24 |
with gr.Blocks() as demo:
|
| 25 |
-
gr.
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
**Required JSON:**
|
| 35 |
-
```json
|
| 36 |
-
{{
|
| 37 |
-
"data": [
|
| 38 |
-
"your message",
|
| 39 |
-
"your-api-key-here"
|
| 40 |
-
]
|
| 41 |
-
}}
|
| 42 |
-
""")
|
| 43 |
|
| 44 |
-
demo.launch(
|
|
|
|
| 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()
|