Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
MODEL_ID = "Qwen/Qwen2.5-Coder-1.5B-Instruct" # or your model
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.float16 if device=="cuda" else torch.float32)
|
| 10 |
+
model.to(device)
|
| 11 |
+
|
| 12 |
+
def generate(prompt):
|
| 13 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 14 |
+
outputs = model.generate(**inputs, max_new_tokens=256, do_sample=True, temperature=0.2)
|
| 15 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 16 |
+
|
| 17 |
+
demo = gr.Interface(fn=generate, inputs="text", outputs="text")
|
| 18 |
+
demo.launch()
|