Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="MarkChenX/lfm2-quantum-128m-sft")
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
response = ""
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
with gr.Blocks() as demo:
|
| 63 |
with gr.Sidebar():
|
|
|
|
| 64 |
gr.LoginButton()
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from collections.abc import Generator
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
+
MODEL_ID = "MarkChenX/lfm2-quantum-128m-sft"
|
| 9 |
+
|
| 10 |
|
| 11 |
def respond(
|
| 12 |
+
message: str,
|
| 13 |
history: list[dict[str, str]],
|
| 14 |
+
system_message: str,
|
| 15 |
+
max_tokens: int,
|
| 16 |
+
temperature: float,
|
| 17 |
+
top_p: float,
|
| 18 |
+
hf_token: gr.OAuthToken | None,
|
| 19 |
+
) -> Generator[str, None, None]:
|
| 20 |
+
"""Generate and stream a response from the Hugging Face model."""
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
if hf_token is None:
|
| 23 |
+
yield "Please sign in with Hugging Face before sending a message."
|
| 24 |
+
return
|
| 25 |
|
| 26 |
+
messages = [
|
| 27 |
+
{
|
| 28 |
+
"role": "system",
|
| 29 |
+
"content": system_message.strip() or "You are a friendly chatbot.",
|
| 30 |
+
},
|
| 31 |
+
*history,
|
| 32 |
+
{"role": "user", "content": message},
|
| 33 |
+
]
|
| 34 |
|
| 35 |
+
client = InferenceClient(
|
| 36 |
+
model=MODEL_ID,
|
| 37 |
+
token=hf_token.token,
|
| 38 |
+
)
|
| 39 |
|
| 40 |
response = ""
|
| 41 |
|
| 42 |
+
try:
|
| 43 |
+
for chunk in client.chat_completion(
|
| 44 |
+
messages=messages,
|
| 45 |
+
max_tokens=int(max_tokens),
|
| 46 |
+
temperature=float(temperature),
|
| 47 |
+
top_p=float(top_p),
|
| 48 |
+
stream=True,
|
| 49 |
+
):
|
| 50 |
+
if not chunk.choices:
|
| 51 |
+
continue
|
| 52 |
+
|
| 53 |
+
token = chunk.choices[0].delta.content or ""
|
| 54 |
+
response += token
|
| 55 |
+
yield response
|
| 56 |
+
|
| 57 |
+
except Exception as exc:
|
| 58 |
+
# Keep the existing partial response visible if generation fails.
|
| 59 |
+
error_message = f"\n\nGeneration failed: {exc}"
|
| 60 |
+
yield response + error_message
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
with gr.Blocks(title="LFM2 Quantum Chat") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
with gr.Sidebar():
|
| 65 |
+
gr.Markdown("### Authentication")
|
| 66 |
gr.LoginButton()
|
| 67 |
+
|
| 68 |
+
gr.ChatInterface(
|
| 69 |
+
fn=respond,
|
| 70 |
+
additional_inputs=[
|
| 71 |
+
gr.Textbox(
|
| 72 |
+
value="You are a friendly chatbot.",
|
| 73 |
+
label="System message",
|
| 74 |
+
lines=3,
|
| 75 |
+
),
|
| 76 |
+
gr.Slider(
|
| 77 |
+
minimum=1,
|
| 78 |
+
maximum=2048,
|
| 79 |
+
value=512,
|
| 80 |
+
step=1,
|
| 81 |
+
label="Max new tokens",
|
| 82 |
+
),
|
| 83 |
+
gr.Slider(
|
| 84 |
+
minimum=0.1,
|
| 85 |
+
maximum=4.0,
|
| 86 |
+
value=0.7,
|
| 87 |
+
step=0.1,
|
| 88 |
+
label="Temperature",
|
| 89 |
+
),
|
| 90 |
+
gr.Slider(
|
| 91 |
+
minimum=0.1,
|
| 92 |
+
maximum=1.0,
|
| 93 |
+
value=0.95,
|
| 94 |
+
step=0.05,
|
| 95 |
+
label="Top-p",
|
| 96 |
+
),
|
| 97 |
+
],
|
| 98 |
+
)
|
| 99 |
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
# Spaces already exposes the application publicly.
|
| 103 |
+
# Disabling SSR avoids launching the experimental Node.js SSR process.
|
| 104 |
+
demo.launch(
|
| 105 |
+
share=False,
|
| 106 |
+
ssr_mode=False,
|
| 107 |
+
)
|