Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Model loading with optimized settings
|
| 6 |
+
MODEL_NAME = "Qwen/Qwen3-0.6B"
|
| 7 |
+
cache_dir = "./model_cache"
|
| 8 |
+
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 10 |
+
MODEL_NAME,
|
| 11 |
+
trust_remote_code=True,
|
| 12 |
+
cache_dir=cache_dir
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
+
MODEL_NAME,
|
| 17 |
+
trust_remote_code=True,
|
| 18 |
+
torch_dtype=torch.float16,
|
| 19 |
+
device_map="auto",
|
| 20 |
+
cache_dir=cache_dir
|
| 21 |
+
).eval()
|
| 22 |
+
|
| 23 |
+
# Create text generation pipeline
|
| 24 |
+
text_generator = pipeline(
|
| 25 |
+
"text-generation",
|
| 26 |
+
model=model,
|
| 27 |
+
tokenizer=tokenizer,
|
| 28 |
+
device=0 if torch.cuda.is_available() else -1,
|
| 29 |
+
pad_token_id=tokenizer.eos_token_id
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
def generate_response(prompt, max_new_tokens=256, temperature=0.7, top_p=0.9):
|
| 33 |
+
"""Generate response with safe defaults"""
|
| 34 |
+
try:
|
| 35 |
+
response = text_generator(
|
| 36 |
+
prompt,
|
| 37 |
+
max_new_tokens=int(max_new_tokens),
|
| 38 |
+
temperature=float(temperature),
|
| 39 |
+
top_p=float(top_p),
|
| 40 |
+
do_sample=True,
|
| 41 |
+
truncation=True
|
| 42 |
+
)
|
| 43 |
+
return response[0]["generated_text"]
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Error: {str(e)}"
|
| 46 |
+
|
| 47 |
+
# Gradio interface with advanced settings
|
| 48 |
+
with gr.Blocks(theme="soft", title="Qwen3-0.6B Chat Interface") as demo:
|
| 49 |
+
gr.Markdown("# 🧠 Qwen3-0.6B Text-to-Text Chat")
|
| 50 |
+
gr.Markdown("Powered by HuggingFace Transformers and Gradio")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
with gr.Column():
|
| 54 |
+
prompt = gr.Textbox(
|
| 55 |
+
label="User Input",
|
| 56 |
+
placeholder="Ask me anything...",
|
| 57 |
+
lines=5
|
| 58 |
+
)
|
| 59 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 60 |
+
max_new_tokens = gr.Slider(
|
| 61 |
+
minimum=32,
|
| 62 |
+
maximum=512,
|
| 63 |
+
value=256,
|
| 64 |
+
step=32,
|
| 65 |
+
label="Max New Tokens"
|
| 66 |
+
)
|
| 67 |
+
temperature = gr.Slider(
|
| 68 |
+
minimum=0.1,
|
| 69 |
+
maximum=1.0,
|
| 70 |
+
value=0.7,
|
| 71 |
+
step=0.1,
|
| 72 |
+
label="Temperature"
|
| 73 |
+
)
|
| 74 |
+
top_p = gr.Slider(
|
| 75 |
+
minimum=0.1,
|
| 76 |
+
maximum=1.0,
|
| 77 |
+
value=0.9,
|
| 78 |
+
step=0.1,
|
| 79 |
+
label="Top-p Sampling"
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
with gr.Column():
|
| 83 |
+
output = gr.Textbox(label="Model Response", lines=10)
|
| 84 |
+
|
| 85 |
+
submit = gr.Button("💬 Generate Response")
|
| 86 |
+
submit.click(
|
| 87 |
+
fn=generate_response,
|
| 88 |
+
inputs=[prompt, max_new_tokens, temperature, top_p],
|
| 89 |
+
outputs=output
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
gr.Examples(
|
| 93 |
+
examples=[
|
| 94 |
+
["Explain quantum computing in simple terms"],
|
| 95 |
+
["Write a poem about autumn leaves"],
|
| 96 |
+
["Solve this math problem: 2x + 5 = 17"]
|
| 97 |
+
],
|
| 98 |
+
inputs=prompt
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
demo.launch()
|