Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,33 +6,59 @@ import faiss
|
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
# Set the Hugging Face API Key
|
| 9 |
-
GrOQ_API_KEY= "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"
|
| 10 |
-
client=Groq(api_key=GrOQ_API_KEY)
|
| 11 |
|
| 12 |
# Supported Microcontrollers/Processors
|
| 13 |
devices = {
|
| 14 |
"Arduino": {
|
| 15 |
"template": """
|
| 16 |
-
// Arduino Syntax Template
|
| 17 |
void setup() {
|
| 18 |
// Setup code here, to run once:
|
|
|
|
| 19 |
}
|
| 20 |
-
|
| 21 |
void loop() {
|
| 22 |
// Main code here, to run repeatedly:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
}
|
| 24 |
""",
|
| 25 |
},
|
| 26 |
"PIC18": {
|
| 27 |
"template": """
|
| 28 |
// PIC18 Syntax Template
|
|
|
|
| 29 |
#include <xc.h>
|
| 30 |
void main() {
|
| 31 |
-
//
|
| 32 |
while(1) {
|
| 33 |
-
//
|
|
|
|
|
|
|
|
|
|
| 34 |
}
|
| 35 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
""",
|
| 37 |
},
|
| 38 |
"8085": {
|
|
@@ -58,7 +84,7 @@ def tokenize_and_chunk_code(code, model_name="gpt2", max_length=512):
|
|
| 58 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 59 |
tokens = tokenizer(code, truncation=True, return_tensors="pt")
|
| 60 |
chunks = [
|
| 61 |
-
tokens.input_ids[0][i
|
| 62 |
for i in range(0, len(tokens.input_ids[0]), max_length)
|
| 63 |
]
|
| 64 |
return chunks
|
|
@@ -73,31 +99,75 @@ def store_code_in_faiss(chunks):
|
|
| 73 |
|
| 74 |
# Streamlit App
|
| 75 |
def main():
|
| 76 |
-
|
| 77 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
# Step
|
| 83 |
-
st.subheader("Coding Syntax Template")
|
| 84 |
-
st.code(devices[device]["template"])
|
| 85 |
|
| 86 |
-
# Step
|
| 87 |
-
prompt = st.
|
| 88 |
|
| 89 |
if prompt:
|
| 90 |
-
st.
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
| 100 |
|
| 101 |
# Run the Streamlit App
|
| 102 |
if __name__ == "__main__":
|
| 103 |
-
main()
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
# Set the Hugging Face API Key
|
| 9 |
+
GrOQ_API_KEY = "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"
|
| 10 |
+
client = Groq(api_key=GrOQ_API_KEY)
|
| 11 |
|
| 12 |
# Supported Microcontrollers/Processors
|
| 13 |
devices = {
|
| 14 |
"Arduino": {
|
| 15 |
"template": """
|
| 16 |
+
// Arduino Syntax Template (for Arduino Uno)
|
| 17 |
void setup() {
|
| 18 |
// Setup code here, to run once:
|
| 19 |
+
pinMode(13, OUTPUT);
|
| 20 |
}
|
|
|
|
| 21 |
void loop() {
|
| 22 |
// Main code here, to run repeatedly:
|
| 23 |
+
digitalWrite(13, HIGH); // Turn on LED
|
| 24 |
+
delay(1000); // Wait for a second
|
| 25 |
+
digitalWrite(13, LOW); // Turn off LED
|
| 26 |
+
delay(1000); // Wait for a second
|
| 27 |
}
|
| 28 |
""",
|
| 29 |
},
|
| 30 |
"PIC18": {
|
| 31 |
"template": """
|
| 32 |
// PIC18 Syntax Template
|
| 33 |
+
// C Language:
|
| 34 |
#include <xc.h>
|
| 35 |
void main() {
|
| 36 |
+
TRISB = 0; // Set PORTB as output
|
| 37 |
while(1) {
|
| 38 |
+
LATB = 0xFF; // Turn on all LEDs on PORTB
|
| 39 |
+
__delay_ms(500); // Delay for 500ms
|
| 40 |
+
LATB = 0x00; // Turn off all LEDs
|
| 41 |
+
__delay_ms(500); // Delay for 500ms
|
| 42 |
}
|
| 43 |
}
|
| 44 |
+
// Assembly Language:
|
| 45 |
+
; PIC18 Assembly Language Template
|
| 46 |
+
ORG 0x00
|
| 47 |
+
START:
|
| 48 |
+
BCF STATUS, RP0 ; Select Bank 0
|
| 49 |
+
MOVLW 0xFF ; Load 0xFF into WREG (all LEDs on)
|
| 50 |
+
MOVWF PORTB ; Move WREG value to PORTB
|
| 51 |
+
CALL Delay ; Call delay subroutine
|
| 52 |
+
CLRF PORTB ; Clear PORTB (turn off LEDs)
|
| 53 |
+
CALL Delay ; Call delay subroutine
|
| 54 |
+
GOTO START ; Repeat
|
| 55 |
+
Delay:
|
| 56 |
+
MOVLW 0xFF ; Load WREG with delay value
|
| 57 |
+
MOVWF 0x30 ; Store in memory location
|
| 58 |
+
DELAY_LOOP:
|
| 59 |
+
DECFSZ 0x30, F ; Decrement and check if zero
|
| 60 |
+
GOTO DELAY_LOOP ; Repeat until zero
|
| 61 |
+
RETURN
|
| 62 |
""",
|
| 63 |
},
|
| 64 |
"8085": {
|
|
|
|
| 84 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 85 |
tokens = tokenizer(code, truncation=True, return_tensors="pt")
|
| 86 |
chunks = [
|
| 87 |
+
tokens.input_ids[0][i: i + max_length]
|
| 88 |
for i in range(0, len(tokens.input_ids[0]), max_length)
|
| 89 |
]
|
| 90 |
return chunks
|
|
|
|
| 99 |
|
| 100 |
# Streamlit App
|
| 101 |
def main():
|
| 102 |
+
# Custom styling for the app using markdown
|
| 103 |
+
st.markdown("""
|
| 104 |
+
<style>
|
| 105 |
+
body {
|
| 106 |
+
background: linear-gradient(to right, #ff7e5f, #feb47b, #6a11cb, #2575fc);
|
| 107 |
+
font-family: Arial, sans-serif;
|
| 108 |
+
}
|
| 109 |
+
.title {
|
| 110 |
+
font-size: 40px;
|
| 111 |
+
font-weight: bold;
|
| 112 |
+
color: #FF6347;
|
| 113 |
+
}
|
| 114 |
+
.subheader {
|
| 115 |
+
font-size: 24px;
|
| 116 |
+
color: #4CAF50;
|
| 117 |
+
}
|
| 118 |
+
.generated-code {
|
| 119 |
+
background-color: #f0f0f0;
|
| 120 |
+
padding: 10px;
|
| 121 |
+
border-radius: 10px;
|
| 122 |
+
}
|
| 123 |
+
.sidebar .sidebar-content {
|
| 124 |
+
background-color: #f4f4f4;
|
| 125 |
+
}
|
| 126 |
+
.stButton button {
|
| 127 |
+
background-color: #008CBA;
|
| 128 |
+
color: white;
|
| 129 |
+
border-radius: 8px;
|
| 130 |
+
padding: 10px;
|
| 131 |
+
font-size: 16px;
|
| 132 |
+
}
|
| 133 |
+
</style>
|
| 134 |
+
""", unsafe_allow_html=True)
|
| 135 |
+
|
| 136 |
+
st.title("Microcontroller Code Generator", anchor="title")
|
| 137 |
+
|
| 138 |
+
# Step 1: Display Custom Image
|
| 139 |
+
# Use the image URL from Hugging Face Files Section
|
| 140 |
+
image_url = "https://huggingface.co/spaces/sunnynazir/Embed_Code_Generator_Modified/resolve/main/1.png"
|
| 141 |
+
st.image(image_url, caption="Custom Image", use_container_width=True)
|
| 142 |
+
|
| 143 |
+
st.write("Select a microcontroller and provide your code requirements to generate code.")
|
| 144 |
|
| 145 |
+
# Sidebar with different selection options for users
|
| 146 |
+
with st.sidebar:
|
| 147 |
+
st.header("Microcontroller Selection")
|
| 148 |
+
device = st.selectbox("Select a microcontroller/processor", options=["Arduino", "PIC18", "8085"])
|
| 149 |
+
st.write("Your selected microcontroller: ", device)
|
| 150 |
|
| 151 |
+
# Step 1: Show Syntax Template based on selected device
|
| 152 |
+
st.subheader("Coding Syntax Template", anchor="subheader")
|
| 153 |
+
st.code(devices[device]["template"], language="c" if device != "8085" else "asm")
|
| 154 |
|
| 155 |
+
# Step 2: Get User Prompt and Generate Code
|
| 156 |
+
prompt = st.text_area("Enter your code requirements:", height=100)
|
| 157 |
|
| 158 |
if prompt:
|
| 159 |
+
if st.button("Generate Code"):
|
| 160 |
+
st.write("Generating code, please wait...")
|
| 161 |
+
code = generate_code(f"Write {device} code for: {prompt}")
|
| 162 |
+
|
| 163 |
+
# Step 3: Chunk, Tokenize, and Store Code in FAISS
|
| 164 |
+
chunks = tokenize_and_chunk_code(code)
|
| 165 |
+
store_code_in_faiss(chunks)
|
| 166 |
+
|
| 167 |
+
# Step 4: Display Generated Code
|
| 168 |
+
st.subheader("Generated Code", anchor="generated-code")
|
| 169 |
+
st.code(code)
|
| 170 |
|
| 171 |
# Run the Streamlit App
|
| 172 |
if __name__ == "__main__":
|
| 173 |
+
main()
|