CODEXspace / app.py
DEADLOCK007X's picture
Update app.py
2b461c6 verified
raw
history blame
2.01 kB
import gradio as gr
import json
def evaluate_code(question, code, language):
# Check if code is empty
if not code.strip():
return json.dumps({
"stars": 0,
"feedback": "No code provided. Please submit your code solution."
})
# Build a prompt for the evaluator (this is where you would normally call TinyLlama)
prompt = f"""
You are an expert code evaluator.
Rate the user's solution to the following problem from 0-5 (0 = completely incorrect, 5 = excellent).
Also provide a concise "feedback" message.
Programming Language: {language}
Problem: "{question}"
Solution: "{code}"
Return ONLY valid JSON: {{"stars": number, "feedback": string}}
Do not include any extra text outside the JSON.
"""
# For demonstration purposes, we simulate the evaluation:
# (Replace the following with a call to your TinyLlama model)
if language.lower() == "python":
result = {"stars": 5, "feedback": "Excellent Python solution with clear logic and proper syntax."}
elif language.lower() == "c":
result = {"stars": 5, "feedback": "Great C code! The solution is correct with optimal use of functions and pointers."}
elif language.lower() == "java":
result = {"stars": 5, "feedback": "Excellent Java solution with proper class structure and error handling."}
else:
result = {"stars": 0, "feedback": "Language not supported."}
return json.dumps(result)
iface = gr.Interface(
fn=evaluate_code,
inputs=[
gr.Textbox(lines=2, placeholder="Enter the problem question here...", label="Question"),
gr.Code(language="python", label="Code"),
gr.Dropdown(choices=["c", "python", "java"], label="Language", value="python")
],
outputs=gr.Textbox(label="Evaluation Result"),
title="TinyLlama Code Evaluator",
description="Enter a coding question, your solution, and select a programming language to get AI-powered feedback."
)
if __name__ == "__main__":
iface.launch()