Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,33 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
client = InferenceClient(
|
| 6 |
-
|
| 7 |
-
|
| 8 |
)
|
| 9 |
|
|
|
|
|
|
|
| 10 |
def chatbot(message, history):
|
| 11 |
-
messages = [{"role": "
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
return response.choices[0].message.content
|
| 14 |
|
| 15 |
-
demo = gr.ChatInterface(fn=chatbot)
|
| 16 |
|
| 17 |
-
demo.launch(
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# Read token from Hugging Face Space Secrets
|
| 6 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 7 |
+
|
| 8 |
client = InferenceClient(
|
| 9 |
+
provider="hf-inference",
|
| 10 |
+
api_key=HF_TOKEN,
|
| 11 |
)
|
| 12 |
|
| 13 |
+
SYSTEM_PROMPT = "You are a helpful assistant."
|
| 14 |
+
|
| 15 |
def chatbot(message, history):
|
| 16 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 17 |
+
|
| 18 |
+
for user, assistant in history:
|
| 19 |
+
messages.append({"role": "user", "content": user})
|
| 20 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 21 |
+
|
| 22 |
+
messages.append({"role": "user", "content": message})
|
| 23 |
+
|
| 24 |
+
response = client.chat.completions.create(
|
| 25 |
+
model="microsoft/Phi-3-mini-4k-instruct",
|
| 26 |
+
messages=messages,
|
| 27 |
+
max_tokens=100,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
return response.choices[0].message.content
|
| 31 |
|
| 32 |
+
demo = gr.ChatInterface(fn=chatbot, title="Simple HF Chatbot")
|
| 33 |
|
| 34 |
+
demo.launch()
|