Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,79 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
"""
|
| 43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 44 |
-
"""
|
| 45 |
-
demo = gr.ChatInterface(
|
| 46 |
-
respond,
|
| 47 |
-
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
-
gr.Slider(
|
| 52 |
-
minimum=0.1,
|
| 53 |
-
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
| 3 |
+
import torch
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Model loading with memory optimization
|
| 7 |
+
cache_dir = "./model_cache"
|
| 8 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 9 |
+
|
| 10 |
+
model_name = "Qwen/Qwen1.5-0.5B"
|
| 11 |
+
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 13 |
+
model_name,
|
| 14 |
+
cache_dir=cache_dir,
|
| 15 |
+
trust_remote_code=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
+
model_name,
|
| 20 |
+
torch_dtype=torch.float16,
|
| 21 |
+
cache_dir=cache_dir,
|
| 22 |
+
trust_remote_code=True
|
| 23 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 24 |
+
|
| 25 |
+
# Generation configuration
|
| 26 |
+
generation_config = GenerationConfig.from_pretrained(model_name)
|
| 27 |
+
|
| 28 |
+
def generate_text(prompt, temperature, max_new_tokens):
|
| 29 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 30 |
+
|
| 31 |
+
with torch.inference_mode():
|
| 32 |
+
outputs = model.generate(
|
| 33 |
+
**inputs,
|
| 34 |
+
max_new_tokens=int(max_new_tokens),
|
| 35 |
+
temperature=float(temperature),
|
| 36 |
+
pad_token_id=tokenizer.eos_token_id
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
response = tokenizer.decode(outputs[0][len(inputs["input_ids"][0]):], skip_special_tokens=True)
|
| 40 |
+
return response.strip()
|
| 41 |
+
|
| 42 |
+
# Gradio interface
|
| 43 |
+
with gr.Blocks(theme="soft", title="Qwen Text Generation") as demo:
|
| 44 |
+
gr.Markdown("# 🧠 Qwen1.5-0.5B Text Generation")
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
with gr.Column():
|
| 48 |
+
prompt = gr.Textbox(
|
| 49 |
+
label="Input Prompt",
|
| 50 |
+
placeholder="Enter your instruction or question...",
|
| 51 |
+
lines=5
|
| 52 |
+
)
|
| 53 |
+
temperature = gr.Slider(
|
| 54 |
+
minimum=0.1, maximum=2.0, value=0.7, step=0.1,
|
| 55 |
+
label="Creativity (Temperature)"
|
| 56 |
+
)
|
| 57 |
+
max_new_tokens = gr.Slider(
|
| 58 |
+
minimum=50, maximum=1000, value=200, step=50,
|
| 59 |
+
label="Max New Tokens"
|
| 60 |
+
)
|
| 61 |
+
generate_btn = gr.Button("✨ Generate", variant="primary")
|
| 62 |
+
|
| 63 |
+
with gr.Column():
|
| 64 |
+
output = gr.Textbox(label="Model Response", lines=10, interactive=False)
|
| 65 |
+
|
| 66 |
+
generate_btn.click(
|
| 67 |
+
fn=generate_text,
|
| 68 |
+
inputs=[prompt, temperature, max_new_tokens],
|
| 69 |
+
outputs=output
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
gr.Markdown("""
|
| 73 |
+
### ℹ️ Tips
|
| 74 |
+
- Higher **temperature** = more creative/chaotic responses
|
| 75 |
+
- Lower **temperature** = more deterministic answers
|
| 76 |
+
- Adjust **max tokens** for longer/shorter outputs
|
| 77 |
+
""")
|
| 78 |
|
| 79 |
+
demo.launch()
|
|
|