Harshal Vhatkar
commited on
Commit
·
1eb8c2b
1
Parent(s):
abaedf3
integrate code playground
Browse files- app.py +1 -1
- code_playground.py +126 -0
- session_page.py +7 -2
app.py
CHANGED
@@ -20,7 +20,7 @@ import json
|
|
20 |
from bson import ObjectId
|
21 |
client = OpenAI(api_key=os.getenv("OPENAI_KEY"))
|
22 |
from dotenv import load_dotenv
|
23 |
-
|
24 |
load_dotenv()
|
25 |
# PERPLEXITY_API_KEY = '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'
|
26 |
|
|
|
20 |
from bson import ObjectId
|
21 |
client = OpenAI(api_key=os.getenv("OPENAI_KEY"))
|
22 |
from dotenv import load_dotenv
|
23 |
+
from code_playground import display_code_playground
|
24 |
load_dotenv()
|
25 |
# PERPLEXITY_API_KEY = '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'
|
26 |
|
code_playground.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import time
|
3 |
+
import streamlit as st
|
4 |
+
from code_editor import code_editor
|
5 |
+
import requests
|
6 |
+
import json
|
7 |
+
from streamlit_ace import st_ace
|
8 |
+
import subprocess
|
9 |
+
import os
|
10 |
+
import tempfile
|
11 |
+
import dotenv
|
12 |
+
from dotenv import load_dotenv
|
13 |
+
load_dotenv()
|
14 |
+
|
15 |
+
# Code 2:
|
16 |
+
# Supported languages and their file extensions
|
17 |
+
# Judge0 API configuration
|
18 |
+
|
19 |
+
JUDGE0_API_URL = "https://judge0-ce.p.rapidapi.com/submissions"
|
20 |
+
JUDGE0_API_KEY = "065c9c9b12mshe3bfa8aa9549b16p11954ajsnda1e6cb80ec4"# Replace with your RapidAPI key
|
21 |
+
HEADERS = {
|
22 |
+
"X-RapidAPI-Key": JUDGE0_API_KEY,
|
23 |
+
"X-RapidAPI-Host": "judge0-ce.p.rapidapi.com",
|
24 |
+
"Content-Type": "application/json",
|
25 |
+
}
|
26 |
+
|
27 |
+
# Supported languages and their Judge0 language IDs
|
28 |
+
LANGUAGES = {
|
29 |
+
"C": 50,
|
30 |
+
"C++": 54,
|
31 |
+
"Java": 62,
|
32 |
+
"Python": 71,
|
33 |
+
"JavaScript": 63,
|
34 |
+
}
|
35 |
+
|
36 |
+
# Function to execute code using Judge0 API
|
37 |
+
def execute_code(code, language_id):
|
38 |
+
# Create a submission
|
39 |
+
# code 3:
|
40 |
+
# Function to execute code using Judge0 API
|
41 |
+
# Encode the code in base64
|
42 |
+
encoded_code = base64.b64encode(code.encode("utf-8")).decode("utf-8")
|
43 |
+
|
44 |
+
# Create a submission
|
45 |
+
data = {
|
46 |
+
"source_code": encoded_code,
|
47 |
+
"language_id": language_id,
|
48 |
+
"base64_encoded": True, # Indicate that the code is base64 encoded
|
49 |
+
}
|
50 |
+
response = requests.post(
|
51 |
+
f"{JUDGE0_API_URL}?base64_encoded=true&wait=false",
|
52 |
+
headers=HEADERS,
|
53 |
+
json=data,
|
54 |
+
)
|
55 |
+
if response.status_code != 201:
|
56 |
+
return f"Error: Unable to create submission. Status code: {response.status_code}, Response: {response.text}"
|
57 |
+
|
58 |
+
submission_token = response.json()["token"]
|
59 |
+
|
60 |
+
# Poll for the result
|
61 |
+
while True:
|
62 |
+
result_response = requests.get(
|
63 |
+
f"{JUDGE0_API_URL}/{submission_token}?base64_encoded=true",
|
64 |
+
headers=HEADERS,
|
65 |
+
)
|
66 |
+
if result_response.status_code != 200:
|
67 |
+
return f"Error: Unable to fetch result. Status code: {result_response.status_code}, Response: {result_response.text}"
|
68 |
+
|
69 |
+
result = result_response.json()
|
70 |
+
if result["status"]["id"] not in [1, 2]: # Status 1 = in queue, 2 = processing
|
71 |
+
break
|
72 |
+
time.sleep(1) # Wait before polling again
|
73 |
+
|
74 |
+
# Parse the result
|
75 |
+
if result["status"]["id"] == 3: # Status 3 = accepted
|
76 |
+
output = result["stdout"] if result["stdout"] else "No output"
|
77 |
+
if output:
|
78 |
+
# Decode the output from base64
|
79 |
+
output = base64.b64decode(output).decode("utf-8")
|
80 |
+
return output
|
81 |
+
else:
|
82 |
+
error = result["stderr"] if result["stderr"] else result["compile_output"]
|
83 |
+
if error:
|
84 |
+
# Decode the error from base64
|
85 |
+
error = base64.b64decode(error).decode("utf-8")
|
86 |
+
return error
|
87 |
+
|
88 |
+
# Map languages to Ace Editor language modes
|
89 |
+
ACE_LANGUAGE_MODES = {
|
90 |
+
"C": "c_cpp",
|
91 |
+
"C++": "c_cpp",
|
92 |
+
"Java": "java",
|
93 |
+
"Python": "python",
|
94 |
+
"JavaScript": "javascript",
|
95 |
+
}
|
96 |
+
|
97 |
+
def display_code_playground(session, student_id, course_id):
|
98 |
+
st.markdown("#### Code Playground")
|
99 |
+
|
100 |
+
# Language selection
|
101 |
+
language = st.selectbox("Select Language", list(LANGUAGES.keys()))
|
102 |
+
|
103 |
+
# Code editor
|
104 |
+
code = st_ace(
|
105 |
+
placeholder="Write your code here...",
|
106 |
+
language=ACE_LANGUAGE_MODES[language],
|
107 |
+
theme="clouds_midnight",
|
108 |
+
key="code-editor",
|
109 |
+
font_size=14,
|
110 |
+
tab_size=4,
|
111 |
+
wrap=True,
|
112 |
+
show_gutter=True,
|
113 |
+
show_print_margin=True,
|
114 |
+
auto_update=False,
|
115 |
+
height=300,
|
116 |
+
)
|
117 |
+
|
118 |
+
# Run button
|
119 |
+
if st.button("Run"):
|
120 |
+
if code:
|
121 |
+
st.markdown("##### Output")
|
122 |
+
with st.spinner("Executing code..."):
|
123 |
+
output = execute_code(code, LANGUAGES[language])
|
124 |
+
st.code(output, language="text")
|
125 |
+
else:
|
126 |
+
st.warning("Please write some code before running.")
|
session_page.py
CHANGED
@@ -32,6 +32,7 @@ from analytics import derive_analytics, create_embeddings, cosine_similarity
|
|
32 |
from bs4 import BeautifulSoup
|
33 |
import streamlit.components.v1 as components
|
34 |
from live_chat_feature import display_live_chat_interface
|
|
|
35 |
|
36 |
load_dotenv()
|
37 |
MONGO_URI = os.getenv('MONGO_URI')
|
@@ -2386,9 +2387,10 @@ def display_session_content(student_id, course_id, session, username, user_type)
|
|
2386 |
"Quizzes",
|
2387 |
"Subjective Tests",
|
2388 |
"Group Work",
|
2389 |
-
"End Terms"
|
|
|
2390 |
])
|
2391 |
-
if len(tabs) <=
|
2392 |
with tabs[0]:
|
2393 |
display_preclass_content(session, student_id, course_id)
|
2394 |
with tabs[1]:
|
@@ -2405,6 +2407,9 @@ def display_session_content(student_id, course_id, session, username, user_type)
|
|
2405 |
with tabs[6]:
|
2406 |
st.subheader("End Terms")
|
2407 |
st.info("End term content will be available soon.")
|
|
|
|
|
|
|
2408 |
else:
|
2409 |
st.error("Error creating tabs. Please try again.")
|
2410 |
|
|
|
32 |
from bs4 import BeautifulSoup
|
33 |
import streamlit.components.v1 as components
|
34 |
from live_chat_feature import display_live_chat_interface
|
35 |
+
from code_playground import display_code_playground
|
36 |
|
37 |
load_dotenv()
|
38 |
MONGO_URI = os.getenv('MONGO_URI')
|
|
|
2387 |
"Quizzes",
|
2388 |
"Subjective Tests",
|
2389 |
"Group Work",
|
2390 |
+
"End Terms",
|
2391 |
+
"Code Playground"
|
2392 |
])
|
2393 |
+
if len(tabs) <= 8:
|
2394 |
with tabs[0]:
|
2395 |
display_preclass_content(session, student_id, course_id)
|
2396 |
with tabs[1]:
|
|
|
2407 |
with tabs[6]:
|
2408 |
st.subheader("End Terms")
|
2409 |
st.info("End term content will be available soon.")
|
2410 |
+
with tabs[7]:
|
2411 |
+
# st.subheader("Code Playground")
|
2412 |
+
display_code_playground(session, student_id, course_id)
|
2413 |
else:
|
2414 |
st.error("Error creating tabs. Please try again.")
|
2415 |
|