Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,68 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
def respond(
|
| 6 |
-
message,
|
| 7 |
history: list[dict[str, str]],
|
| 8 |
-
system_message,
|
| 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 |
messages.extend(history)
|
| 22 |
-
|
| 23 |
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
response = ""
|
| 26 |
|
| 27 |
-
for
|
| 28 |
-
messages,
|
| 29 |
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
temperature=temperature,
|
| 32 |
top_p=top_p,
|
|
|
|
| 33 |
):
|
| 34 |
-
choices
|
| 35 |
-
|
| 36 |
-
|
| 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(
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
gr.Slider(
|
| 54 |
minimum=0.1,
|
| 55 |
maximum=1.0,
|
|
@@ -65,6 +78,5 @@ with gr.Blocks() as demo:
|
|
| 65 |
gr.LoginButton()
|
| 66 |
chatbot.render()
|
| 67 |
|
| 68 |
-
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from typing import Generator
|
| 4 |
|
| 5 |
|
| 6 |
def respond(
|
| 7 |
+
message: str,
|
| 8 |
history: list[dict[str, str]],
|
| 9 |
+
system_message: str,
|
| 10 |
+
max_tokens: int,
|
| 11 |
+
temperature: float,
|
| 12 |
+
top_p: float,
|
| 13 |
hf_token: gr.OAuthToken,
|
| 14 |
+
) -> Generator[str, None, None]:
|
| 15 |
"""
|
| 16 |
+
Streaming chat response using Hugging Face Inference API.
|
| 17 |
+
Docs:
|
| 18 |
+
https://huggingface.co/docs/huggingface_hub/en/guides/inference
|
| 19 |
"""
|
|
|
|
| 20 |
|
| 21 |
+
client = InferenceClient(
|
| 22 |
+
model="openai/gpt-oss-20b",
|
| 23 |
+
token=hf_token.token,
|
| 24 |
+
)
|
| 25 |
|
| 26 |
+
messages = [{"role": "system", "content": system_message}]
|
| 27 |
messages.extend(history)
|
|
|
|
| 28 |
messages.append({"role": "user", "content": message})
|
| 29 |
|
| 30 |
response = ""
|
| 31 |
|
| 32 |
+
for chunk in client.chat_completion(
|
| 33 |
+
messages=messages,
|
| 34 |
max_tokens=max_tokens,
|
|
|
|
| 35 |
temperature=temperature,
|
| 36 |
top_p=top_p,
|
| 37 |
+
stream=True,
|
| 38 |
):
|
| 39 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 40 |
+
response += chunk.choices[0].delta.content
|
| 41 |
+
yield response
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
chatbot = gr.ChatInterface(
|
| 45 |
+
fn=respond,
|
| 46 |
type="messages",
|
| 47 |
additional_inputs=[
|
| 48 |
+
gr.Textbox(
|
| 49 |
+
value="You are a friendly Chatbot.",
|
| 50 |
+
label="System message",
|
| 51 |
+
),
|
| 52 |
+
gr.Slider(
|
| 53 |
+
minimum=1,
|
| 54 |
+
maximum=2048,
|
| 55 |
+
value=512,
|
| 56 |
+
step=1,
|
| 57 |
+
label="Max new tokens",
|
| 58 |
+
),
|
| 59 |
+
gr.Slider(
|
| 60 |
+
minimum=0.1,
|
| 61 |
+
maximum=4.0,
|
| 62 |
+
value=0.7,
|
| 63 |
+
step=0.1,
|
| 64 |
+
label="Temperature",
|
| 65 |
+
),
|
| 66 |
gr.Slider(
|
| 67 |
minimum=0.1,
|
| 68 |
maximum=1.0,
|
|
|
|
| 78 |
gr.LoginButton()
|
| 79 |
chatbot.render()
|
| 80 |
|
|
|
|
| 81 |
if __name__ == "__main__":
|
| 82 |
demo.launch()
|