Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,58 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
#
|
| 5 |
MODEL_CHOICES = {
|
| 6 |
"Mistral 7B Instruct (mistralai/Mistral-7B-Instruct-v0.2)": "mistralai/Mistral-7B-Instruct-v0.2",
|
| 7 |
"Falcon 7B Instruct (tiiuae/falcon-7b-instruct)": "tiiuae/falcon-7b-instruct",
|
| 8 |
"Zephyr 7B Beta (HuggingFaceH4/zephyr-7b-beta)": "HuggingFaceH4/zephyr-7b-beta",
|
| 9 |
}
|
| 10 |
|
|
|
|
| 11 |
def complete_text(
|
| 12 |
prompt: str,
|
| 13 |
max_tokens: int,
|
| 14 |
temperature: float,
|
| 15 |
top_p: float,
|
| 16 |
model_choice: str,
|
| 17 |
-
hf_token: gr.OAuthToken,
|
| 18 |
):
|
| 19 |
"""
|
| 20 |
-
|
| 21 |
"""
|
| 22 |
if not prompt:
|
| 23 |
-
yield "⚠️
|
| 24 |
return
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
token_str = getattr(hf_token, "token", None)
|
| 30 |
-
if token_str is None:
|
| 31 |
-
yield "🔐 This Space uses the Hugging Face Inference API. Please click **Login** (left sidebar) to authorize."
|
| 32 |
return
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
|
|
|
|
| 36 |
try:
|
| 37 |
-
generated = ""
|
| 38 |
-
# text_generation returns an iterator of string chunks when stream=True
|
| 39 |
for chunk in client.text_generation(
|
| 40 |
prompt=prompt,
|
| 41 |
max_new_tokens=int(max_tokens),
|
| 42 |
temperature=float(temperature),
|
| 43 |
top_p=float(top_p),
|
| 44 |
-
stream=True,
|
| 45 |
repetition_penalty=1.0,
|
|
|
|
| 46 |
):
|
| 47 |
generated += chunk
|
| 48 |
yield generated
|
| 49 |
except Exception as e:
|
| 50 |
-
yield f"❌
|
| 51 |
|
| 52 |
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
-
gr.Markdown("## ✍️ Text Completion Demo (Hugging Face Inference API)")
|
| 55 |
gr.Markdown(
|
| 56 |
-
"Pick a model, enter a prompt, and
|
| 57 |
-
"Some models require
|
| 58 |
)
|
| 59 |
|
| 60 |
with gr.Row():
|
|
@@ -72,13 +70,13 @@ with gr.Blocks() as demo:
|
|
| 72 |
value=list(MODEL_CHOICES.keys())[0],
|
| 73 |
label="Choose a model",
|
| 74 |
)
|
| 75 |
-
submit = gr.Button("Generate")
|
| 76 |
with gr.Column(scale=3):
|
| 77 |
with gr.Sidebar():
|
| 78 |
-
login = gr.LoginButton() #
|
| 79 |
output = gr.Textbox(label="Generated Completion", lines=15)
|
| 80 |
|
| 81 |
-
# Wire
|
| 82 |
submit.click(
|
| 83 |
fn=complete_text,
|
| 84 |
inputs=[prompt, max_tokens, temperature, top_p, model_choice, login],
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Three open-source models to choose from
|
| 5 |
MODEL_CHOICES = {
|
| 6 |
"Mistral 7B Instruct (mistralai/Mistral-7B-Instruct-v0.2)": "mistralai/Mistral-7B-Instruct-v0.2",
|
| 7 |
"Falcon 7B Instruct (tiiuae/falcon-7b-instruct)": "tiiuae/falcon-7b-instruct",
|
| 8 |
"Zephyr 7B Beta (HuggingFaceH4/zephyr-7b-beta)": "HuggingFaceH4/zephyr-7b-beta",
|
| 9 |
}
|
| 10 |
|
| 11 |
+
|
| 12 |
def complete_text(
|
| 13 |
prompt: str,
|
| 14 |
max_tokens: int,
|
| 15 |
temperature: float,
|
| 16 |
top_p: float,
|
| 17 |
model_choice: str,
|
| 18 |
+
hf_token: gr.OAuthToken, # token provided after user logs in
|
| 19 |
):
|
| 20 |
"""
|
| 21 |
+
Generate a text completion from a Hugging Face model, streamed chunk by chunk.
|
| 22 |
"""
|
| 23 |
if not prompt:
|
| 24 |
+
yield "⚠️ Please enter a prompt."
|
| 25 |
return
|
| 26 |
|
| 27 |
+
# If the user has not logged in, warn them (needed for gated models like Mistral / LLaMA)
|
| 28 |
+
if not getattr(hf_token, "token", None):
|
| 29 |
+
yield "🔐 Please login with your Hugging Face account (see left sidebar)."
|
|
|
|
|
|
|
|
|
|
| 30 |
return
|
| 31 |
|
| 32 |
+
model_id = MODEL_CHOICES[model_choice]
|
| 33 |
+
client = InferenceClient(model=model_id, token=hf_token.token)
|
| 34 |
|
| 35 |
+
generated = ""
|
| 36 |
try:
|
|
|
|
|
|
|
| 37 |
for chunk in client.text_generation(
|
| 38 |
prompt=prompt,
|
| 39 |
max_new_tokens=int(max_tokens),
|
| 40 |
temperature=float(temperature),
|
| 41 |
top_p=float(top_p),
|
|
|
|
| 42 |
repetition_penalty=1.0,
|
| 43 |
+
stream=True,
|
| 44 |
):
|
| 45 |
generated += chunk
|
| 46 |
yield generated
|
| 47 |
except Exception as e:
|
| 48 |
+
yield f"❌ Error while generating: {e}"
|
| 49 |
|
| 50 |
|
| 51 |
with gr.Blocks() as demo:
|
| 52 |
+
gr.Markdown("## ✍️ Text Completion Demo (Open-Source LLMs via Hugging Face Inference API)")
|
| 53 |
gr.Markdown(
|
| 54 |
+
"Pick a model, enter a prompt, and stream completions. "
|
| 55 |
+
"Some models require logging in (click **Login** in the sidebar)."
|
| 56 |
)
|
| 57 |
|
| 58 |
with gr.Row():
|
|
|
|
| 70 |
value=list(MODEL_CHOICES.keys())[0],
|
| 71 |
label="Choose a model",
|
| 72 |
)
|
| 73 |
+
submit = gr.Button("Generate Completion")
|
| 74 |
with gr.Column(scale=3):
|
| 75 |
with gr.Sidebar():
|
| 76 |
+
login = gr.LoginButton() # supplies OAuth token at runtime
|
| 77 |
output = gr.Textbox(label="Generated Completion", lines=15)
|
| 78 |
|
| 79 |
+
# Wire inputs (6 args) to match function params (6 args)
|
| 80 |
submit.click(
|
| 81 |
fn=complete_text,
|
| 82 |
inputs=[prompt, max_tokens, temperature, top_p, model_choice, login],
|