Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
|
| 6 |
+
MODELS = {
|
| 7 |
+
"Qwen2.5-0.5B-Instruct": "Qwen/Qwen2.5-0.5B-Instruct",
|
| 8 |
+
"Llama-3.2-1B-Instruct": "meta-llama/Llama-3.2-1B-Instruct",
|
| 9 |
+
"Qwen2.5-1B-Instruct": "Qwen/Qwen2.5-1B-Instruct",
|
| 10 |
+
"Gemma-3-1B-IT": "google/gemma-3-1b-it",
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
loaded = {}
|
| 14 |
+
|
| 15 |
+
def load_model(model_name):
|
| 16 |
+
if model_name not in loaded:
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(MODELS[model_name])
|
| 18 |
+
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
MODELS[model_name],
|
| 21 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 22 |
+
device_map="auto",
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
loaded[model_name] = (tokenizer, model)
|
| 26 |
+
|
| 27 |
+
return loaded[model_name]
|
| 28 |
+
|
| 29 |
+
def generate(model_name, prompt):
|
| 30 |
+
tokenizer, model = load_model(model_name)
|
| 31 |
+
|
| 32 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 33 |
+
|
| 34 |
+
outputs = model.generate(
|
| 35 |
+
**inputs,
|
| 36 |
+
max_new_tokens=256,
|
| 37 |
+
temperature=0.7,
|
| 38 |
+
do_sample=True,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 42 |
+
|
| 43 |
+
demo = gr.Interface(
|
| 44 |
+
fn=generate,
|
| 45 |
+
inputs=[
|
| 46 |
+
gr.Dropdown(list(MODELS.keys()), value="Qwen2.5-0.5B-Instruct"),
|
| 47 |
+
gr.Textbox(lines=8, label="Prompt"),
|
| 48 |
+
],
|
| 49 |
+
outputs=gr.Textbox(lines=12, label="Response"),
|
| 50 |
+
title="Micro Tier LLMs",
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
demo.launch()
|