Haseeb-001 commited on
Commit
655e71d
Β·
verified Β·
1 Parent(s): ed169ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -49
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
- try:
32
- chat_completion = client.chat.completions.create(
33
- messages=[
34
- {
35
- "role": "user",
36
- "content": f"Explain the following code descriptively and attractively so the user can easily understand it:\n\n{code}",
37
- }
38
- ],
39
- model="llama3-8b-8192",
40
- stream=False,
41
- )
42
- explanation = chat_completion.choices[0].message.content
43
- return explanation
44
- except Exception as e:
45
- st.error(f"Error explaining code: {e}")
46
- return ""
47
 
 
48
  def save_code_as_pdf(code, file_name="generated_code.pdf"):
49
- pdf = FPDF()
50
- pdf.add_page()
51
- pdf.set_font("Arial", size=12)
52
- pdf.multi_cell(0, 10, code)
53
- pdf.output(file_name)
54
- return file_name
55
 
56
- # ... rest of your code ...
 
57
 
58
- if st.button("Generate Code"):
59
- # ... your code to generate code ...
60
- if generated_code:
61
- # ... your code to display generated code ...
62
 
63
- # Explanation Button
64
- if st.button("Explain Code"):
65
- explanation = explain_code(generated_code)
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")