Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748import gradio as grimport jsondef evaluate_code(question, code, language):# Check if code is emptyif 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__":12345678910111213141516171819202122232425262728293031import gradio as grdef evaluate_code(language, question, code):# Check if code is providedif not code.strip():return "Error: No code provided. Please enter your solution code."# Dummy evaluation logic based on language.# Replace this with your TinyLlama integration later.if language.lower() == "c":return "C evaluation: The code meets basic criteria but may need improvements."elif language.lower() == "python":return "Python evaluation: Code is correct and follows best practices."elif language.lower() == "java":return "Java evaluation: Good structure, but consider error handling improvements."else:return "Unsupported language. Please select C, Python, or Java."iface = gr.Interface(fn=evaluate_code,inputs=[gr.Dropdown(choices=["C", "Python", "Java"], label="Language"),gr.Textbox(lines=2, placeholder="Enter the problem question here...", label="Question"),gr.Code(label="Your Code (if any)") # No placeholder property here.],outputs=gr.Textbox(label="Evaluation Result"),title="Code Evaluator",description="Enter a coding question and your solution (if available) to get AI-powered feedback. Supports C, Python, and Java.")if __name__ == "__main__":
app.py+16-33