Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
@@ -9,45 +10,58 @@ def respond(
|
|
| 9 |
max_tokens,
|
| 10 |
temperature,
|
| 11 |
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
"""
|
| 15 |
-
|
|
|
|
| 16 |
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
response = ""
|
| 26 |
|
| 27 |
-
|
|
|
|
| 28 |
messages,
|
| 29 |
max_tokens=max_tokens,
|
| 30 |
stream=True,
|
| 31 |
temperature=temperature,
|
| 32 |
top_p=top_p,
|
| 33 |
):
|
| 34 |
-
choices =
|
| 35 |
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
token = choices[0].delta.content
|
| 38 |
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
|
| 43 |
-
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
chatbot = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
type="messages",
|
| 49 |
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a
|
| 51 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
gr.Slider(
|
|
@@ -63,8 +77,10 @@ chatbot = gr.ChatInterface(
|
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
with gr.Sidebar():
|
| 65 |
gr.LoginButton()
|
|
|
|
| 66 |
chatbot.render()
|
| 67 |
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
|
|
|
| 10 |
max_tokens,
|
| 11 |
temperature,
|
| 12 |
top_p,
|
| 13 |
+
hf_token: gr.OAuthToken | None,
|
| 14 |
):
|
| 15 |
"""
|
| 16 |
+
Chat handler for Hugging Face Inference API.
|
| 17 |
+
Works with both HF Spaces (OAuth) and local execution (HF_TOKEN env var).
|
| 18 |
"""
|
|
|
|
| 19 |
|
| 20 |
+
# β
Get token from Spaces OAuth or local env
|
| 21 |
+
token = None
|
| 22 |
+
if hf_token and getattr(hf_token, "token", None):
|
| 23 |
+
token = hf_token.token
|
| 24 |
+
else:
|
| 25 |
+
token = os.getenv("HF_TOKEN")
|
| 26 |
|
| 27 |
+
if not token:
|
| 28 |
+
yield "β No Hugging Face token found. Please log in or set HF_TOKEN."
|
| 29 |
+
return
|
| 30 |
+
|
| 31 |
+
# β
Choose a model that supports Inference API
|
| 32 |
+
model_id = "mistralai/Mistral-7B-Instruct-v0.2" # change if needed
|
| 33 |
+
client = InferenceClient(token=token, model=model_id)
|
| 34 |
|
| 35 |
+
# β
Build messages list
|
| 36 |
+
messages = [{"role": "system", "content": system_message}]
|
| 37 |
+
messages.extend(history)
|
| 38 |
messages.append({"role": "user", "content": message})
|
| 39 |
|
| 40 |
response = ""
|
| 41 |
|
| 42 |
+
# β
Streaming response
|
| 43 |
+
for message_chunk in client.chat_completion(
|
| 44 |
messages,
|
| 45 |
max_tokens=max_tokens,
|
| 46 |
stream=True,
|
| 47 |
temperature=temperature,
|
| 48 |
top_p=top_p,
|
| 49 |
):
|
| 50 |
+
choices = message_chunk.choices
|
| 51 |
token = ""
|
| 52 |
+
if len(choices) and choices[0].delta and choices[0].delta.content:
|
| 53 |
token = choices[0].delta.content
|
| 54 |
|
| 55 |
response += token
|
| 56 |
yield response
|
| 57 |
|
| 58 |
|
| 59 |
+
# β
Gradio UI
|
|
|
|
|
|
|
| 60 |
chatbot = gr.ChatInterface(
|
| 61 |
respond,
|
| 62 |
type="messages",
|
| 63 |
additional_inputs=[
|
| 64 |
+
gr.Textbox(value="You are a helpful research assistant.", label="System message"),
|
| 65 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 66 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 67 |
gr.Slider(
|
|
|
|
| 77 |
with gr.Blocks() as demo:
|
| 78 |
with gr.Sidebar():
|
| 79 |
gr.LoginButton()
|
| 80 |
+
gr.Markdown("π Log in with your Hugging Face account to run the model.")
|
| 81 |
chatbot.render()
|
| 82 |
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
| 85 |
+
# β
share=True β generates public link when running locally
|
| 86 |
+
demo.launch(share=True)
|