Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,4 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
-
import torch
|
| 4 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
MODEL_ID = os.environ.get("HF_MODEL_ID", "teamaMohamed115/smollm-360m-code-lora")
|
| 8 |
-
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Safe loader: try with device_map for HF inference if possible
|
| 12 |
-
print(f"Loading tokenizer and model from {MODEL_ID} on {DEVICE}")
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, use_fast=True)
|
| 16 |
-
# If the model was pushed with custom config (like trusting remote code), we handle gracefully
|
| 17 |
-
try:
|
| 18 |
-
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 19 |
-
except Exception:
|
| 20 |
-
model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
model.to(DEVICE)
|
| 24 |
-
model.eval()
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# Generation helper
|
| 28 |
-
GEN_KWARGS = dict(
|
| 29 |
-
max_new_tokens=256,
|
| 30 |
-
do_sample=True,
|
| 31 |
-
temperature=0.2,
|
| 32 |
-
top_p=0.95,
|
| 33 |
top_k=50,
|
| 34 |
num_return_sequences=1,
|
| 35 |
)
|
|
@@ -82,4 +51,35 @@ return decoded.strip()
|
|
| 82 |
|
| 83 |
with gr.Blocks(title="SmolLM Python Code Assistant") as demo:
|
| 84 |
gr.Markdown("# SmolLM β Python Code Generation\nEnter an instruction and get a multi-function Python module.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
top_k=50,
|
| 3 |
num_return_sequences=1,
|
| 4 |
)
|
|
|
|
| 51 |
|
| 52 |
with gr.Blocks(title="SmolLM Python Code Assistant") as demo:
|
| 53 |
gr.Markdown("# SmolLM β Python Code Generation\nEnter an instruction and get a multi-function Python module.")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
with gr.Row():
|
| 57 |
+
instr = gr.Textbox(lines=6, placeholder="Describe the Python module you want...", label="Instruction")
|
| 58 |
+
with gr.Column(scale=1):
|
| 59 |
+
max_t = gr.Slider(minimum=32, maximum=1024, value=256, step=32, label="Max new tokens")
|
| 60 |
+
temp = gr.Slider(minimum=0.0, maximum=1.0, value=0.2, step=0.05, label="Temperature")
|
| 61 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.01, label="Top-p")
|
| 62 |
+
run_btn = gr.Button("Generate")
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
output = gr.Code(label="Generated Python module", language="python")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def run(instruction, max_tokens, temperature, top_p):
|
| 69 |
+
try:
|
| 70 |
+
return generate_code(instruction, max_tokens, temperature, top_p)
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return f"Error during generation: {e}"
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
run_btn.click(run, inputs=[instr, max_t, temp, top_p], outputs=[output])
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
gr.Examples(examples=[
|
| 79 |
+
"Implement a Python module that includes: a function to compute Fibonacci sequence, a function to check primality, and a function to compute factorial, all with type hints and docstrings.",
|
| 80 |
+
"Create a Python module for basic matrix operations (add, multiply, transpose) with appropriate error handling and tests.",
|
| 81 |
+
], inputs=instr)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
demo.launch()
|