Spaces:
Build error
Build error
Upload 5 files
Browse files- Backend.py +21 -0
- frontend.py +93 -0
- interpreter.py +19 -0
- logger.py +26 -0
- req.txt +9 -0
Backend.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import google.generativeai as generativeai
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
|
5 |
+
# Load API key from environment
|
6 |
+
load_dotenv()
|
7 |
+
generativeai.configure(api_key=os.getenv("GOOGLE_GEMINI_KEY"))
|
8 |
+
|
9 |
+
def get_correction_and_comments(code_snippet):
|
10 |
+
prompt = [
|
11 |
+
"Analyze and correct the following Python code, add comments, and format it:",
|
12 |
+
code_snippet
|
13 |
+
]
|
14 |
+
response = generativeai.GenerativeModel('gemini-pro').generate_content(prompt)
|
15 |
+
return response.text if response else "No suggestions available."
|
16 |
+
|
17 |
+
def generate_questions(question):
|
18 |
+
prompt = ["You are Python coding assistant generate only question and answer based of the given code"
|
19 |
+
, question]
|
20 |
+
response = generativeai.GenerativeModel('gemini-pro').generate_content(prompt)
|
21 |
+
return response.text if response else "No answer available."
|
frontend.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from Backend import get_correction_and_comments, generate_questions
|
3 |
+
from logger import logger
|
4 |
+
from pymongo import MongoClient, errors
|
5 |
+
import interpreter
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
import os
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
# Configure the page
|
12 |
+
st.set_page_config(page_title="Interactive Code Assistant", layout="wide")
|
13 |
+
st.title("✨ Interactive Code Assistant with Python Interpreter ✨")
|
14 |
+
|
15 |
+
# Initialize session state for feedback to persist data
|
16 |
+
if "helpful" not in st.session_state:
|
17 |
+
st.session_state.helpful = None # Initialize feedback as None
|
18 |
+
|
19 |
+
# Set up MongoDB connection
|
20 |
+
try:
|
21 |
+
client = MongoClient(os.getenv("MONGO_URI"), serverSelectionTimeoutMS=5000)
|
22 |
+
client.server_info() # Trigger an exception if MongoDB is not reachable
|
23 |
+
db = client["Capstone"]
|
24 |
+
feedback_collection = db["Feedback"]
|
25 |
+
except errors.ServerSelectionTimeoutError:
|
26 |
+
st.error("Error: Could not connect to MongoDB. Please ensure MongoDB is running.")
|
27 |
+
|
28 |
+
# Create two expanded columns for wider side-by-side text areas
|
29 |
+
colu1, colu2 = st.columns([1, 1]) # Both columns have equal width
|
30 |
+
|
31 |
+
# Text area in the first column for entering code
|
32 |
+
with colu1:
|
33 |
+
st.subheader("Code Input")
|
34 |
+
code_input = st.text_area("Enter Your Python Code:", height=400, max_chars=10000)
|
35 |
+
|
36 |
+
# Text area in the second column to display the output from Google Gemini
|
37 |
+
with colu2:
|
38 |
+
st.subheader("Corrected Output")
|
39 |
+
gemini_output = st.empty()
|
40 |
+
|
41 |
+
# Buttons for different functionalities
|
42 |
+
col1, col2, col3 = st.columns([0.3, 0.3, 0.3])
|
43 |
+
with col1:
|
44 |
+
if st.button("Run Code"):
|
45 |
+
try:
|
46 |
+
output = interpreter.run_code(code_input)
|
47 |
+
st.subheader("✨ Code Output ✨")
|
48 |
+
st.write(output)
|
49 |
+
except Exception as e:
|
50 |
+
st.error(f"Error: {e}")
|
51 |
+
logger.error(f"Code execution error: {e}")
|
52 |
+
|
53 |
+
with col2:
|
54 |
+
if st.button("Generate Questions"):
|
55 |
+
# Generate questions automatically, displayed below the text boxes
|
56 |
+
generated_questions = generate_questions(code_input)
|
57 |
+
st.subheader("🤖 Model-Generated Questions")
|
58 |
+
st.write(f"Raw Response: {generated_questions}")
|
59 |
+
|
60 |
+
|
61 |
+
with col3:
|
62 |
+
if st.button("Corrected Code"):
|
63 |
+
logger.info("User requested code correction.")
|
64 |
+
corrected_code = get_correction_and_comments(code_input)
|
65 |
+
gemini_output.code(corrected_code, language="python")
|
66 |
+
|
67 |
+
# Feedback form (outside of the columns, after all content)
|
68 |
+
st.subheader("Feedback")
|
69 |
+
st.session_state.helpful = st.radio("Were the questions helpful?", ("Yes", "No"))
|
70 |
+
|
71 |
+
if st.button("Submit Feedback"):
|
72 |
+
if st.session_state.helpful is not None:
|
73 |
+
try:
|
74 |
+
feedback_collection.insert_one({"helpful": st.session_state.helpful})
|
75 |
+
st.success("Feedback submitted successfully.")
|
76 |
+
except Exception as e:
|
77 |
+
st.error(f"Failed to submit feedback: {e}")
|
78 |
+
# Hide Streamlit's default menu and style adjustments for a cleaner look
|
79 |
+
st.markdown(
|
80 |
+
"""
|
81 |
+
<style>
|
82 |
+
.reportview-container .main .block-container {
|
83 |
+
padding-top: 1rem;
|
84 |
+
padding-bottom: 1rem;
|
85 |
+
max-width: 1200px;
|
86 |
+
}
|
87 |
+
.stTextArea {
|
88 |
+
font-size: 14px;
|
89 |
+
}
|
90 |
+
</style>
|
91 |
+
""",
|
92 |
+
unsafe_allow_html=True
|
93 |
+
)
|
interpreter.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import sys
|
3 |
+
from logger import logger
|
4 |
+
|
5 |
+
def run_code(code):
|
6 |
+
# Redirect stdout to capture code output
|
7 |
+
old_stdout = sys.stdout
|
8 |
+
redirected_output = sys.stdout = io.StringIO()
|
9 |
+
|
10 |
+
try:
|
11 |
+
exec(code)
|
12 |
+
except Exception as e:
|
13 |
+
logger.error(f"Execution error: {e}")
|
14 |
+
return f"Error: {e}"
|
15 |
+
finally:
|
16 |
+
# Reset stdout
|
17 |
+
sys.stdout = old_stdout
|
18 |
+
|
19 |
+
return redirected_output.getvalue()
|
logger.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
import pymongo
|
3 |
+
from datetime import datetime
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
import os
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
# MongoDB setup
|
10 |
+
client = pymongo.MongoClient(os.getenv("MONGO_URI"))
|
11 |
+
db = client["Capstone"]
|
12 |
+
log_collection = db["logs"]
|
13 |
+
|
14 |
+
class MongoHandler(logging.Handler):
|
15 |
+
def emit(self, record):
|
16 |
+
log = {
|
17 |
+
"level": record.levelname,
|
18 |
+
"message": record.getMessage(),
|
19 |
+
"timestamp": datetime.now()
|
20 |
+
}
|
21 |
+
log_collection.insert_one(log)
|
22 |
+
|
23 |
+
# Configure logging
|
24 |
+
logging.basicConfig(level=logging.INFO)
|
25 |
+
logger = logging.getLogger("ProjectLogger")
|
26 |
+
logger.addHandler(MongoHandler())
|
req.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
googlegenrativeai
|
2 |
+
requests
|
3 |
+
numpy
|
4 |
+
pandas
|
5 |
+
torch
|
6 |
+
huggingface
|
7 |
+
logging
|
8 |
+
maths
|
9 |
+
tktinker
|