Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -27,56 +27,72 @@ def generate_code(summary, language):
|
|
27 |
st.error(f"Error generating code: {e}")
|
28 |
return ""
|
29 |
|
|
|
30 |
def explain_code(code):
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
|
|
48 |
def save_code_as_pdf(code, file_name="generated_code.pdf"):
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
|
56 |
-
#
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
if generated_code:
|
61 |
-
# ... your code to display generated code ...
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
st.subheader("π Code Explanation:")
|
67 |
-
st.write(explanation)
|
68 |
-
|
69 |
-
# Download Code as PDF
|
70 |
-
if st.button("Download Code as PDF"):
|
71 |
-
pdf_path = save_code_as_pdf(modified_code) # Use modified_code or generated_code based on your preference
|
72 |
-
with open(pdf_path, "rb") as pdf_file:
|
73 |
-
st.download_button(
|
74 |
-
label="π₯ Download PDF",
|
75 |
-
data=pdf_file,
|
76 |
-
file_name="generated_code.pdf",
|
77 |
-
mime="application/pdf",
|
78 |
-
)
|
79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
# New Code Button
|
82 |
if st.button("Generate New Code"):
|
@@ -84,9 +100,4 @@ if st.button("Generate New Code"):
|
|
84 |
|
85 |
# Footer Information
|
86 |
st.markdown("---")
|
87 |
-
st.write("π Powered by **Streamlit**, **Groq**, and **StarCoder** | Deployed on Hugging Face")
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
27 |
st.error(f"Error generating code: {e}")
|
28 |
return ""
|
29 |
|
30 |
+
# Function to explain the generated code using Llama
|
31 |
def explain_code(code):
|
32 |
+
try:
|
33 |
+
chat_completion = client.chat.completions.create(
|
34 |
+
messages=[
|
35 |
+
{
|
36 |
+
"role": "user",
|
37 |
+
"content": f"Explain the following code descriptively and attractively so the user can easily understand it:\n\n{code}",
|
38 |
+
}
|
39 |
+
],
|
40 |
+
model="llama3-8b-8192",
|
41 |
+
stream=False,
|
42 |
+
)
|
43 |
+
explanation = chat_completion.choices[0].message.content
|
44 |
+
return explanation
|
45 |
+
except Exception as e:
|
46 |
+
st.error(f"Error explaining code: {e}")
|
47 |
+
return ""
|
48 |
|
49 |
+
# Function to save code as a PDF
|
50 |
def save_code_as_pdf(code, file_name="generated_code.pdf"):
|
51 |
+
pdf = FPDF()
|
52 |
+
pdf.add_page()
|
53 |
+
pdf.set_font("Arial", size=12)
|
54 |
+
pdf.multi_cell(0, 10, code)
|
55 |
+
pdf.output(file_name)
|
56 |
+
return file_name
|
57 |
|
58 |
+
# --- Streamlit Interface ---
|
59 |
+
st.set_page_config(page_title="Generative AI Code Generator", page_icon="π§βπ»", layout="wide")
|
60 |
|
61 |
+
# Page Title
|
62 |
+
st.title("π Generative AI Code Generator Using StarCoder")
|
|
|
|
|
63 |
|
64 |
+
# Input Fields
|
65 |
+
summary = st.text_area("π Enter the Task Summary", "For example: Create a function to add two numbers.")
|
66 |
+
language = st.selectbox("π Select Programming Language", ["Python", "Java", "JavaScript", "C++"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
# Generate Code Button
|
69 |
+
if st.button("Generate Code"):
|
70 |
+
if summary:
|
71 |
+
generated_code = generate_code(summary, language)
|
72 |
+
|
73 |
+
if generated_code:
|
74 |
+
st.subheader(f"β¨ Generated {language} Code:")
|
75 |
+
st.code(generated_code, language=language.lower())
|
76 |
+
|
77 |
+
# Code Modification Section
|
78 |
+
modified_code = st.text_area("βοΈ Modify the Code (Optional):", value=generated_code, height=200)
|
79 |
+
|
80 |
+
# Explanation Button
|
81 |
+
if st.button("Explain Code"):
|
82 |
+
explanation = explain_code(generated_code) # Use generated_code for explanation
|
83 |
+
st.subheader("π Code Explanation:")
|
84 |
+
st.write(explanation)
|
85 |
+
|
86 |
+
# Download Code as PDF
|
87 |
+
if st.button("Download Code as PDF"):
|
88 |
+
pdf_path = save_code_as_pdf(modified_code) # Use modified_code if edited
|
89 |
+
with open(pdf_path, "rb") as pdf_file:
|
90 |
+
st.download_button(
|
91 |
+
label="π₯ Download PDF",
|
92 |
+
data=pdf_file,
|
93 |
+
file_name="generated_code.pdf",
|
94 |
+
mime="application/pdf",
|
95 |
+
)
|
96 |
|
97 |
# New Code Button
|
98 |
if st.button("Generate New Code"):
|
|
|
100 |
|
101 |
# Footer Information
|
102 |
st.markdown("---")
|
103 |
+
st.write("π Powered by **Streamlit**, **Groq**, and **StarCoder** | Deployed on Hugging Face")
|
|
|
|
|
|
|
|
|
|