Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from collections.abc import Generator
|
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
|
|
|
| 8 |
MODEL_ID = "MarkChenX/lfm2-quantum-128m-sft"
|
| 9 |
|
| 10 |
|
|
@@ -17,27 +18,30 @@ def respond(
|
|
| 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
|
| 24 |
return
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
messages = [
|
| 27 |
{
|
| 28 |
"role": "system",
|
| 29 |
-
"content": system_message.strip()
|
|
|
|
| 30 |
},
|
| 31 |
*history,
|
| 32 |
-
{
|
|
|
|
|
|
|
|
|
|
| 33 |
]
|
| 34 |
|
| 35 |
-
|
| 36 |
-
model=MODEL_ID,
|
| 37 |
-
token=hf_token.token,
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
response = ""
|
| 41 |
|
| 42 |
try:
|
| 43 |
for chunk in client.chat_completion(
|
|
@@ -51,18 +55,21 @@ def respond(
|
|
| 51 |
continue
|
| 52 |
|
| 53 |
token = chunk.choices[0].delta.content or ""
|
| 54 |
-
response += token
|
| 55 |
-
yield response
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
with gr.Blocks(title="LFM2 Quantum Chat") as demo:
|
| 64 |
with gr.Sidebar():
|
| 65 |
-
gr.Markdown("##
|
| 66 |
gr.LoginButton()
|
| 67 |
|
| 68 |
gr.ChatInterface(
|
|
@@ -82,7 +89,7 @@ with gr.Blocks(title="LFM2 Quantum Chat") as demo:
|
|
| 82 |
),
|
| 83 |
gr.Slider(
|
| 84 |
minimum=0.1,
|
| 85 |
-
maximum=
|
| 86 |
value=0.7,
|
| 87 |
step=0.1,
|
| 88 |
label="Temperature",
|
|
@@ -99,9 +106,7 @@ with gr.Blocks(title="LFM2 Quantum Chat") as demo:
|
|
| 99 |
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
-
|
| 103 |
-
# Disabling SSR avoids launching the experimental Node.js SSR process.
|
| 104 |
-
demo.launch(
|
| 105 |
share=False,
|
| 106 |
ssr_mode=False,
|
| 107 |
)
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
+
|
| 9 |
MODEL_ID = "MarkChenX/lfm2-quantum-128m-sft"
|
| 10 |
|
| 11 |
|
|
|
|
| 18 |
top_p: float,
|
| 19 |
hf_token: gr.OAuthToken | None,
|
| 20 |
) -> Generator[str, None, None]:
|
|
|
|
|
|
|
| 21 |
if hf_token is None:
|
| 22 |
+
yield "Please sign in with Hugging Face first."
|
| 23 |
return
|
| 24 |
|
| 25 |
+
client = InferenceClient(
|
| 26 |
+
model=MODEL_ID,
|
| 27 |
+
token=hf_token.token,
|
| 28 |
+
timeout=120,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
messages = [
|
| 32 |
{
|
| 33 |
"role": "system",
|
| 34 |
+
"content": system_message.strip()
|
| 35 |
+
or "You are a friendly chatbot.",
|
| 36 |
},
|
| 37 |
*history,
|
| 38 |
+
{
|
| 39 |
+
"role": "user",
|
| 40 |
+
"content": message,
|
| 41 |
+
},
|
| 42 |
]
|
| 43 |
|
| 44 |
+
generated_text = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
try:
|
| 47 |
for chunk in client.chat_completion(
|
|
|
|
| 55 |
continue
|
| 56 |
|
| 57 |
token = chunk.choices[0].delta.content or ""
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
if token:
|
| 60 |
+
generated_text += token
|
| 61 |
+
yield generated_text
|
| 62 |
+
|
| 63 |
+
except Exception as error:
|
| 64 |
+
yield (
|
| 65 |
+
generated_text
|
| 66 |
+
+ f"\n\nInference request failed: {type(error).__name__}: {error}"
|
| 67 |
+
)
|
| 68 |
|
| 69 |
|
| 70 |
with gr.Blocks(title="LFM2 Quantum Chat") as demo:
|
| 71 |
with gr.Sidebar():
|
| 72 |
+
gr.Markdown("## LFM2 Quantum")
|
| 73 |
gr.LoginButton()
|
| 74 |
|
| 75 |
gr.ChatInterface(
|
|
|
|
| 89 |
),
|
| 90 |
gr.Slider(
|
| 91 |
minimum=0.1,
|
| 92 |
+
maximum=2.0,
|
| 93 |
value=0.7,
|
| 94 |
step=0.1,
|
| 95 |
label="Temperature",
|
|
|
|
| 106 |
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
+
demo.queue().launch(
|
|
|
|
|
|
|
| 110 |
share=False,
|
| 111 |
ssr_mode=False,
|
| 112 |
)
|