Haseeb-001 commited on
Commit
c0aa9a8
Β·
verified Β·
1 Parent(s): c557680

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -56
app.py CHANGED
@@ -1,9 +1,8 @@
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)
@@ -18,7 +17,7 @@ def generate_code(summary, language):
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
@@ -27,77 +26,42 @@ def generate_code(summary, language):
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"):
 
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")
 
1
  import os
2
  import streamlit as st
3
  from groq import Groq
 
4
 
5
+ api = "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" # Replace with your actual API key
6
 
7
  # --- Groq API Setup ---
8
  client = Groq(api_key=api)
 
17
  "content": f"Generate efficient {language} code for the following task without any explanations or comments: {summary}",
18
  }
19
  ],
20
+ model="llama3-8b-8192", # Specify the model you want to use
21
  stream=False,
22
  )
23
  generated_code = chat_completion.choices[0].message.content
 
26
  st.error(f"Error generating code: {e}")
27
  return ""
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  # --- Streamlit Interface ---
30
+ st.set_page_config(page_title="Generative AI Code Generator", page_icon="πŸ‘¨β€πŸ’»", layout="wide")
31
 
32
  # Page Title
33
+ st.title("Generative AI Code Generator Using StarCoder")
34
 
35
+ # Summary Input
36
+ summary = st.text_area("Enter the Summary of the Task", "For example: Create a function to add two numbers.")
37
+ language = st.selectbox("Select the Programming Language", ["Python", "Java", "JavaScript", "C++"])
38
 
39
  # Generate Code Button
40
  if st.button("Generate Code"):
41
  if summary:
42
+ # Generate the code using Groq and StarCoder
43
  generated_code = generate_code(summary, language)
44
 
45
  if generated_code:
46
+ st.subheader(f"Generated {language} Code:")
47
  st.code(generated_code, language=language.lower())
48
 
49
+ # Modify or remove sections of code (optional)
50
+ modified_code = st.text_area("Modify the Code (Optional)", value=generated_code, height=200)
 
 
 
 
 
 
51
 
52
+ # Download Code Button (Download as TXT)
53
+ st.download_button(
54
+ label="Download Code",
55
+ data=modified_code or generated_code, # Use modified_code if edited, otherwise generated_code
56
+ file_name="generated_code.txt",
57
+ mime="text/plain"
58
+ )
 
 
 
59
 
60
  # New Code Button
61
  if st.button("Generate New Code"):
62
+ summary = "" # Clear the summary input
63
  st.rerun() # Refresh the page to clear inputs
64
 
65
  # Footer Information
66
  st.markdown("---")
67
+ st.write("πŸ’» Powered by Streamlit | AI Code Generation by Groq and StarCoder")