Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from groq import Groq
|
4 |
+
from fpdf import FPDF # For PDF generation
|
5 |
+
|
6 |
+
api = "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"
|
7 |
+
|
8 |
+
# --- Groq API Setup ---
|
9 |
+
client = Groq(api_key=api)
|
10 |
+
|
11 |
+
# Function to get code from StarCoder model
|
12 |
+
def generate_code(summary, language):
|
13 |
+
try:
|
14 |
+
chat_completion = client.chat.completions.create(
|
15 |
+
messages=[
|
16 |
+
{
|
17 |
+
"role": "user",
|
18 |
+
"content": f"Generate efficient {language} code for the following task without any explanations or comments: {summary}",
|
19 |
+
}
|
20 |
+
],
|
21 |
+
model="llama3-8b-8192", # Specify the model
|
22 |
+
stream=False,
|
23 |
+
)
|
24 |
+
generated_code = chat_completion.choices[0].message.content
|
25 |
+
return generated_code
|
26 |
+
except Exception as e:
|
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)
|
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"):
|
99 |
+
st.rerun() # Refresh the page to clear inputs
|
100 |
+
|
101 |
+
# Footer Information
|
102 |
+
st.markdown("---")
|
103 |
+
st.write("π Powered by **Streamlit**, **Groq**, and **StarCoder** | Deployed on Hugging Face")
|