Views
No views yet
1from transformers import AutoTokenizer, AutoModelForCausalLM
2
3# Load model
4model_name = "MuratKomurcu/starcoder2-stm32"
5tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
6model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
7
8# Generate STM32 code
9prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
10
11### Instruction:
12Create GPIO LED control code
13
14### Input:
15Write STM32 HAL code for LED on GPIOA PIN 5
16
17### Response:
18"""
19
20inputs = tokenizer(prompt, return_tensors="pt")
21outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.2, top_p=0.95)
22code = tokenizer.decode(outputs[0], skip_special_tokens=True)
23print(code.split("### Response:")[-1].strip())
24
25
26Example Output: include stm32f4xx_hal.h. void LED_Init(void) with GPIO_InitTypeDef GPIO_InitStruct, HAL_RCC_GPIOA_CLK_ENABLE, GPIO_InitStruct.Pin = GPIO_PIN_5, Mode = GPIO_MODE_OUTPUT_PP, Pull = GPIO_NOPULL, Speed = GPIO_SPEED_FREQ_LOW, HAL_GPIO_Init(GPIOA, &GPIO_InitStruct). void LED_On(void) calls HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET). void LED_Off(void) calls HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET).
27
28## Supported Peripherals
29
30GPIO for Digital I/O and LED control, UART for Serial communication, ADC for Analog-to-digital conversion, Timer/PWM for Timing and pulse width modulation, I2C for Inter-integrated circuit protocol, SPI for Serial peripheral interface, DMA for Direct memory access, Interrupts for External interrupt handling.
31
32## Limitations
33
34Generated code should be reviewed before production deployment. Clock configurations may need adjustment for specific boards. Advanced DMA configurations require verification. Primarily tested with STM32F4xx family.
35
36## License
37
38This model is released under the BigCode OpenRAIL-M v1 license.