Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
from peft import PeftModel
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# 1. Setup Model IDs
|
| 7 |
+
base_model_id = "unsloth/Qwen2.5-3B-Instruct"
|
| 8 |
+
lora_model_id = "10Aizen01/qwen-2.5-3b-engine-simulator-beta" # Your repo!
|
| 9 |
+
|
| 10 |
+
# 2. Load Tokenizer and Base Model
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_id)
|
| 12 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
+
base_model_id,
|
| 14 |
+
torch_dtype=torch.float16,
|
| 15 |
+
device_map="auto"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# 3. Load your LoRA adapters (The 131MB file)
|
| 19 |
+
model = PeftModel.from_pretrained(base_model, lora_model_id)
|
| 20 |
+
|
| 21 |
+
def generate_engine_code(prompt):
|
| 22 |
+
inputs = tokenizer(f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n", return_tensors="pt").to("cuda")
|
| 23 |
+
outputs = model.generate(**inputs, max_new_tokens=512)
|
| 24 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 25 |
+
|
| 26 |
+
# 4. Create the Web UI
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=generate_engine_code,
|
| 29 |
+
inputs=gr.Textbox(label="Describe your engine (e.g., V8, 4.0L, 9000 RPM)"),
|
| 30 |
+
outputs=gr.Code(label="Generated .mr Script", language="cpp"),
|
| 31 |
+
title="Engine Simulator AI Assistant"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
demo.launch()
|