Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ st.title("AI Code Bot")
|
|
| 7 |
# Introduction
|
| 8 |
st.write("""
|
| 9 |
### Generate Code Snippets with AI
|
| 10 |
-
Enter a programming-related prompt, and the AI will generate a code snippet
|
| 11 |
""")
|
| 12 |
|
| 13 |
# Input box for user query
|
|
@@ -33,10 +33,23 @@ if st.button("Generate Code"):
|
|
| 33 |
with st.spinner("Generating code..."):
|
| 34 |
try:
|
| 35 |
# Generate code using the model
|
| 36 |
-
result = model(user_input, max_length=
|
| 37 |
-
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
st.error(f"An error occurred: {e}")
|
|
|
|
| 7 |
# Introduction
|
| 8 |
st.write("""
|
| 9 |
### Generate Code Snippets with AI
|
| 10 |
+
Enter a programming-related prompt, and the AI will generate a code snippet with explanatory comments.
|
| 11 |
""")
|
| 12 |
|
| 13 |
# Input box for user query
|
|
|
|
| 33 |
with st.spinner("Generating code..."):
|
| 34 |
try:
|
| 35 |
# Generate code using the model
|
| 36 |
+
result = model(user_input, max_length=200, num_return_sequences=1)
|
| 37 |
+
generated_text = result[0]["generated_text"]
|
| 38 |
|
| 39 |
+
# Split comments and code for better presentation
|
| 40 |
+
lines = generated_text.split("\n")
|
| 41 |
+
comments = [line for line in lines if line.strip().startswith("#")]
|
| 42 |
+
code = [line for line in lines if not line.strip().startswith("#")]
|
| 43 |
+
|
| 44 |
+
# Display comments as markdown
|
| 45 |
+
if comments:
|
| 46 |
+
st.subheader("Comments")
|
| 47 |
+
for comment in comments:
|
| 48 |
+
st.markdown(comment)
|
| 49 |
+
|
| 50 |
+
# Display code as Python syntax-highlighted block
|
| 51 |
+
if code:
|
| 52 |
+
st.subheader("Code")
|
| 53 |
+
st.code("\n".join(code), language="python")
|
| 54 |
except Exception as e:
|
| 55 |
st.error(f"An error occurred: {e}")
|