MuratKomurcu commited on
Commit
55bb2bd
·
verified ·
1 Parent(s): 31bc017

Initial commit - STM32 Code Generator UI

Browse files
Files changed (3) hide show
  1. README.md +19 -4
  2. app.py +157 -0
  3. requirements.txt +6 -0
README.md CHANGED
@@ -1,12 +1,27 @@
1
  ---
2
- title: Stm32 Code Generator
3
- emoji: 📈
4
  colorFrom: blue
5
  colorTo: green
6
  sdk: gradio
7
- sdk_version: 5.49.1
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: STM32 Code Generator
3
+ emoji: 🔧
4
  colorFrom: blue
5
  colorTo: green
6
  sdk: gradio
7
+ sdk_version: 4.8.0
8
  app_file: app.py
9
  pinned: false
10
+ license: bigcode-openrail-m
11
  ---
12
 
13
+ # STM32 HAL Code Generator
14
+
15
+ AI-powered code generation for STM32F4 microcontrollers using HAL library.
16
+
17
+ ## Features
18
+ - ✅ 85% accuracy on realistic embedded projects
19
+ - ✅ Supports LED, UART, ADC, PWM, I2C, SPI, Timers, and more
20
+ - ✅ Complete working code with initialization
21
+ - ✅ Fine-tuned on 55,000 STM32 examples
22
+
23
+ ## Model
24
+ Based on [bigcode/starcoder2-3b](https://huggingface.co/bigcode/starcoder2-3b)
25
+
26
+ ## Usage
27
+ Select project type, configure pins, and generate complete STM32 HAL code!
app.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
3
+ from peft import PeftModel
4
+ import torch
5
+
6
+ print("🔄 Model yükleniyor...")
7
+
8
+ # Model yükle
9
+ bnb_config = BitsAndBytesConfig(
10
+ load_in_4bit=True,
11
+ bnb_4bit_quant_type="nf4",
12
+ bnb_4bit_compute_dtype=torch.float16
13
+ )
14
+
15
+ base_model = AutoModelForCausalLM.from_pretrained(
16
+ "bigcode/starcoder2-3b",
17
+ quantization_config=bnb_config,
18
+ device_map="auto",
19
+ trust_remote_code=True
20
+ )
21
+
22
+ model = PeftModel.from_pretrained(base_model, "MuratKomurcu/starcoder2-stm32-turkish")
23
+ tokenizer = AutoTokenizer.from_pretrained("MuratKomurcu/starcoder2-stm32-turkish")
24
+
25
+ print("✅ Model hazır!")
26
+
27
+ def generate_code(project_type, pin_config, parameters, temperature):
28
+ """STM32 kodu üret"""
29
+
30
+ templates = {
31
+ "LED Blink": "Write complete STM32F401RE LED blink code with main function",
32
+ "UART Communication": "Write complete STM32F401RE UART communication code with main function",
33
+ "ADC Reading": "Write complete STM32F401RE ADC reading code with main function",
34
+ "PWM Motor Control": "Write complete STM32F401RE PWM motor control code with main function",
35
+ "I2C Sensor": "Write complete STM32F401RE I2C sensor code with main function",
36
+ "SPI Communication": "Write complete STM32F401RE SPI communication code with main function",
37
+ "Timer Interrupt": "Write complete STM32F401RE timer interrupt code with main function",
38
+ "Temperature Sensor": "Write complete STM32F401RE temperature sensor reading code with main function",
39
+ "Servo Control": "Write complete STM32F401RE servo control code with main function",
40
+ "Watchdog Timer": "Write complete STM32F401RE watchdog timer code with main function",
41
+ }
42
+
43
+ instruction = templates.get(project_type, project_type)
44
+ input_text = f"{pin_config}, {parameters}, include SystemClock_Config"
45
+
46
+ prompt = f"""### Instruction:
47
+ {instruction}
48
+
49
+ ### Input:
50
+ {input_text}
51
+
52
+ ### Response:
53
+ """
54
+
55
+ inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
56
+ outputs = model.generate(
57
+ **inputs,
58
+ max_new_tokens=2048,
59
+ temperature=temperature,
60
+ top_p=0.95,
61
+ do_sample=True,
62
+ pad_token_id=tokenizer.eos_token_id
63
+ )
64
+
65
+ code = tokenizer.decode(outputs[0], skip_special_tokens=True)
66
+ code = code.split("### Response:")[-1].strip()
67
+
68
+ return code
69
+
70
+ # GRADIO UI
71
+ with gr.Blocks(title="STM32 Code Generator", theme=gr.themes.Soft()) as demo:
72
+
73
+ gr.Markdown("""
74
+ # 🔧 STM32 HAL Code Generator
75
+ ### AI-Powered Embedded Code Generation for STM32F4
76
+
77
+ This model generates complete STM32F4 HAL code with **85% accuracy** on realistic projects.
78
+ Fine-tuned on 55,000 STM32 examples.
79
+ """)
80
+
81
+ with gr.Row():
82
+ with gr.Column(scale=1):
83
+ gr.Markdown("### ⚙️ Configuration")
84
+
85
+ project = gr.Dropdown(
86
+ choices=[
87
+ "LED Blink",
88
+ "UART Communication",
89
+ "ADC Reading",
90
+ "PWM Motor Control",
91
+ "I2C Sensor",
92
+ "SPI Communication",
93
+ "Timer Interrupt",
94
+ "Temperature Sensor",
95
+ "Servo Control",
96
+ "Watchdog Timer"
97
+ ],
98
+ label="Project Type",
99
+ value="LED Blink"
100
+ )
101
+
102
+ pins = gr.Textbox(
103
+ label="Pin Configuration",
104
+ placeholder="Example: LED on PA5, Button on PC13",
105
+ value="LED on PA5"
106
+ )
107
+
108
+ params = gr.Textbox(
109
+ label="Additional Parameters",
110
+ placeholder="Example: blink every 1000ms, with debounce",
111
+ value="blink every 1000ms"
112
+ )
113
+
114
+ temp = gr.Slider(
115
+ 0.1, 1.0,
116
+ value=0.3,
117
+ step=0.1,
118
+ label="Temperature (creativity)",
119
+ info="Lower = deterministic, Higher = creative"
120
+ )
121
+
122
+ btn = gr.Button("🚀 Generate Code", variant="primary", size="lg")
123
+
124
+ with gr.Column(scale=2):
125
+ output = gr.Code(
126
+ label="Generated STM32 Code",
127
+ language="c",
128
+ lines=35
129
+ )
130
+
131
+ gr.Markdown("### 💡 Example Projects")
132
+ gr.Examples(
133
+ examples=[
134
+ ["LED Blink", "LED on PA5", "blink every 1000ms", 0.1],
135
+ ["UART Communication", "USART2 on PA2 PA3", "115200 baud, transmit Hello World every second", 0.3],
136
+ ["Temperature Sensor", "ADC1 Channel 0 on PA0", "LM35 sensor (10mV per °C), display via USART2 every 1000ms", 0.1],
137
+ ["PWM Motor Control", "PWM TIM2 Channel 1 on PA5, potentiometer ADC1 on PA0", "control motor speed 0-100%, PWM frequency 1kHz", 0.1],
138
+ ["I2C Sensor", "I2C1", "read from device 0x48, transmit via USART2 115200 baud every 1000ms", 0.1],
139
+ ],
140
+ inputs=[project, pins, params, temp]
141
+ )
142
+
143
+ btn.click(
144
+ fn=generate_code,
145
+ inputs=[project, pins, params, temp],
146
+ outputs=output
147
+ )
148
+
149
+ gr.Markdown("""
150
+ ---
151
+ **Model:** [MuratKomurcu/starcoder2-stm32-turkish](https://huggingface.co/MuratKomurcu/starcoder2-stm32-turkish)
152
+ **Performance:** 17/20 tests passed (85% accuracy)
153
+ **Base:** bigcode/starcoder2-3b fine-tuned on 55K STM32 examples
154
+ **Created by:** Murat Kömürcü
155
+ """)
156
+
157
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ transformers==4.36.0
2
+ peft==0.7.0
3
+ bitsandbytes==0.41.3
4
+ accelerate==0.25.0
5
+ gradio==4.8.0
6
+ torch==2.1.0