Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
def
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 friendly Chatbot.", label="System message"),
|
| 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(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
# Simple Gradio app to run inference with Flan-T5 models (text2text-generation)
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
import torch
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Cache pipelines for models so we don't reload on every request
|
| 10 |
+
PIPES = {}
|
| 11 |
+
|
| 12 |
+
DEFAULT_MODELS = {
|
| 13 |
+
"flan-t5-small": "google/flan-t5-small",
|
| 14 |
+
"flan-t5-base": "google/flan-t5-base",
|
| 15 |
+
"flan-t5-large": "google/flan-t5-large",
|
| 16 |
+
# you can add "flan-t5-xl" or others if your Space has enough RAM/GPU
|
| 17 |
+
}
|
| 18 |
|
| 19 |
+
def get_device():
|
| 20 |
+
return 0 if torch.cuda.is_available() else -1
|
| 21 |
|
| 22 |
+
def get_pipeline(model_key_or_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
+
Returns a transformers pipeline for the given model.
|
| 25 |
+
model_key_or_name: either a key from DEFAULT_MODELS or a full model name.
|
| 26 |
"""
|
| 27 |
+
model_name = DEFAULT_MODELS.get(model_key_or_name, model_key_or_name)
|
| 28 |
+
if model_name in PIPES:
|
| 29 |
+
return PIPES[model_name]
|
| 30 |
+
device = get_device()
|
| 31 |
+
# pipeline will handle tokenizer/model download
|
| 32 |
+
pipe = pipeline(
|
| 33 |
+
"text2text-generation",
|
| 34 |
+
model=model_name,
|
| 35 |
+
tokenizer=model_name,
|
| 36 |
+
device=device,
|
| 37 |
+
# trust_remote_code=False by default; for official Flan-T5 models this is fine
|
| 38 |
+
)
|
| 39 |
+
PIPES[model_name] = pipe
|
| 40 |
+
return pipe
|
| 41 |
|
| 42 |
+
def generate(prompt: str, model_choice: str, max_length: int, temperature: float, num_return_sequences: int):
|
| 43 |
+
"""
|
| 44 |
+
Generate text from the prompt using the selected Flan-T5 model.
|
| 45 |
+
"""
|
| 46 |
+
if not prompt or not prompt.strip():
|
| 47 |
+
return "Please enter a prompt."
|
| 48 |
+
try:
|
| 49 |
+
pipe = get_pipeline(model_choice)
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"Failed to load model {model_choice}: {e}"
|
| 52 |
|
| 53 |
+
# transformers pipeline arguments:
|
| 54 |
+
do_sample = temperature > 0.0
|
| 55 |
+
try:
|
| 56 |
+
outputs = pipe(
|
| 57 |
+
prompt,
|
| 58 |
+
max_length=max_length,
|
| 59 |
+
do_sample=do_sample,
|
| 60 |
+
temperature=float(temperature),
|
| 61 |
+
num_return_sequences=int(num_return_sequences),
|
| 62 |
+
# return_full_text=False is default for text2text-generation
|
| 63 |
+
)
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return f"Generation failed: {e}"
|
| 66 |
|
| 67 |
+
# outputs is a list of dicts with key 'generated_text'
|
| 68 |
+
texts = [o.get("generated_text", "") for o in outputs]
|
| 69 |
+
# Join multiple outputs with separators
|
| 70 |
+
return "\n\n---\n\n".join(texts)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
with gr.Blocks(title="Flan-T5 Inference (Text2Text)") as demo:
|
| 74 |
+
gr.Markdown(
|
| 75 |
+
"""
|
| 76 |
+
# Flan-T5 Text2Text Inference
|
| 77 |
+
Type your prompt and pick a Flan-T5 model. For best performance, enable a GPU in the Space settings.
|
| 78 |
+
"""
|
| 79 |
+
)
|
| 80 |
|
| 81 |
+
with gr.Row():
|
| 82 |
+
with gr.Column(scale=3):
|
| 83 |
+
prompt = gr.Textbox(
|
| 84 |
+
lines=8,
|
| 85 |
+
label="Input prompt",
|
| 86 |
+
placeholder="e.g. Summarize the following article in 2 sentences: ..."
|
| 87 |
+
)
|
| 88 |
+
examples = gr.Examples(
|
| 89 |
+
examples=[
|
| 90 |
+
["Summarize the key points of the American Declaration of Independence."],
|
| 91 |
+
["Translate the following English sentence to French: 'The weather is nice today.'"],
|
| 92 |
+
["Explain in simple terms how photosynthesis works."],
|
| 93 |
+
],
|
| 94 |
+
inputs=prompt
|
| 95 |
+
)
|
| 96 |
+
with gr.Column(scale=1):
|
| 97 |
+
model_choice = gr.Dropdown(list(DEFAULT_MODELS.keys()), value="flan-t5-base", label="Model")
|
| 98 |
+
max_length = gr.Slider(32, 1024, value=256, step=1, label="Max length (tokens)")
|
| 99 |
+
temperature = gr.Slider(0.0, 1.5, value=0.0, step=0.01, label="Temperature (0.0 = deterministic)")
|
| 100 |
+
num_return_sequences = gr.Slider(1, 3, value=1, step=1, label="Number of outputs")
|
| 101 |
+
run_btn = gr.Button("Generate")
|
| 102 |
|
| 103 |
+
output = gr.Textbox(label="Model output", lines=12)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
run_btn.click(
|
| 106 |
+
fn=generate,
|
| 107 |
+
inputs=[prompt, model_choice, max_length, temperature, num_return_sequences],
|
| 108 |
+
outputs=output,
|
| 109 |
+
)
|
| 110 |
|
| 111 |
+
gr.Markdown(
|
| 112 |
+
"""
|
| 113 |
+
Notes:
|
| 114 |
+
- If you want faster generation, set Space to use a GPU (in Settings → Hardware).
|
| 115 |
+
- Larger models (flan-t5-large / flan-t5-xl) need more RAM — they may OOM on CPU or free GPU tiers.
|
| 116 |
+
- You can add other models to DEFAULT_MODELS above or input a full model name from the Hub.
|
| 117 |
+
"""
|
| 118 |
+
)
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
+
demo.launch()
|