teamaMohamed115 commited on
Commit
28c89fd
Β·
verified Β·
1 Parent(s): be63e0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -40
app.py CHANGED
@@ -1,4 +1,34 @@
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  top_k=50,
3
  num_return_sequences=1,
4
  )
@@ -42,44 +72,4 @@ decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
42
  # Strip the prompt prefix from the decoded text if present
43
  if decoded.startswith(prompt):
44
  decoded = decoded[len(prompt):]
45
-
46
-
47
- return decoded.strip()
48
-
49
-
50
-
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()
 
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
+ model = AutoModelForCausalLM.from_pretrained(MODEL_ID, trust_remote_code=True)
18
+ except Exception:
19
+ model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
20
+
21
+
22
+ model.to(DEVICE)
23
+ model.eval()
24
+
25
+
26
+ # Generation helper
27
+ GEN_KWARGS = dict(
28
+ max_new_tokens=256,
29
+ do_sample=True,
30
+ temperature=0.2,
31
+ top_p=0.95,
32
  top_k=50,
33
  num_return_sequences=1,
34
  )
 
72
  # Strip the prompt prefix from the decoded text if present
73
  if decoded.startswith(prompt):
74
  decoded = decoded[len(prompt):]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  demo.launch()