Update app.py
Browse files
app.py
CHANGED
@@ -37,4 +37,40 @@ with gr.Blocks(title="Simple Qwen Test") as demo:
|
|
37 |
)
|
38 |
|
39 |
# Launch the app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
demo.launch()
|
|
|
37 |
)
|
38 |
|
39 |
# Launch the app
|
40 |
+
def test_qwen_text():
|
41 |
+
try:
|
42 |
+
# Use Qwen model with 4-bit quantization to reduce memory usage
|
43 |
+
model_id = "Qwen/Qwen2-7B-Instruct"
|
44 |
+
|
45 |
+
result = []
|
46 |
+
result.append("Loading tokenizer...")
|
47 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
48 |
+
|
49 |
+
result.append("Loading model with quantization...")
|
50 |
+
from transformers import BitsAndBytesConfig
|
51 |
+
|
52 |
+
quantization_config = BitsAndBytesConfig(
|
53 |
+
load_in_4bit=True,
|
54 |
+
bnb_4bit_compute_dtype=torch.float16,
|
55 |
+
bnb_4bit_quant_type="nf4"
|
56 |
+
)
|
57 |
+
|
58 |
+
model = AutoModelForCausalLM.from_pretrained(
|
59 |
+
model_id,
|
60 |
+
quantization_config=quantization_config,
|
61 |
+
device_map="auto"
|
62 |
+
)
|
63 |
+
|
64 |
+
result.append("Generating text...")
|
65 |
+
prompt = "Write a short poem about AI."
|
66 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
67 |
+
outputs = model.generate(**inputs, max_new_tokens=50)
|
68 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
69 |
+
|
70 |
+
result.append(f"Generated text: {generated_text}")
|
71 |
+
result.append("Qwen text model test successful!")
|
72 |
+
|
73 |
+
return "\n".join(result)
|
74 |
+
except Exception as e:
|
75 |
+
return f"Error: {str(e)}\n\n{traceback.format_exc()}"
|
76 |
demo.launch()
|