atz21 commited on
Commit
d58419a
Β·
verified Β·
1 Parent(s): 483a0e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -33
app.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  import gradio as gr
3
  import google.generativeai as genai
4
 
5
- # βœ… Configure Gemini with Hugging Face Secret
6
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
7
 
8
  # ---------- PROMPTS ----------
@@ -34,44 +34,54 @@ Rules:
34
  6. Do not give marks outside the markscheme.
35
  Output must be a structured grading report with reasoning.
36
  """
37
- def grade_student(qp_file, ms_file, ans_file):
38
- try:
39
- # Get file paths
40
- qp_path = qp_file.name
41
- ms_path = ms_file.name
42
- ans_path = ans_file.name
43
 
44
- # Upload to Gemini
45
- qp_uploaded = genai.upload_file(path=qp_path, display_name="Question Paper")
46
- ms_uploaded = genai.upload_file(path=ms_path, display_name="Marking Scheme")
47
- ans_uploaded = genai.upload_file(path=ans_path, display_name="Answer Sheet")
48
 
49
  model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
 
 
 
 
 
 
50
 
51
- transcription_resp = model.generate_content([TRANSCRIPTION_PROMPT, ans_uploaded])
52
- transcription = getattr(transcription_resp, "text", None) or transcription_resp.candidates[0].content.parts[0].text
 
 
 
53
 
54
- grading_resp = model.generate_content([GRADING_PROMPT, qp_uploaded, ms_uploaded, transcription])
55
- grading = getattr(grading_resp, "text", None) or grading_resp.candidates[0].content.parts[0].text
56
 
57
- return transcription, grading
 
58
  except Exception as e:
59
- return f"❌ Error during processing: {e}", ""
60
-
61
- demo = gr.Interface(
62
- fn=grade_student,
63
- inputs=[
64
- gr.File(label="Upload Question Paper (PDF)", type="filepath"),
65
- gr.File(label="Upload Mark Scheme (PDF)", type="filepath"),
66
- gr.File(label="Upload Student Answer Sheet (PDF)", type="filepath"),
67
- ],
68
- outputs=[
69
- gr.Markdown(label="Student Transcription"),
70
- gr.Textbox(label="Grading Report")
71
- ],
72
- title="πŸ“˜ AI Teacher Assistant",
73
- description="Upload a Question Paper, Mark Scheme, and Student Answer Sheet. The app transcribes and grades answers."
74
- )
 
 
 
 
 
 
75
 
76
  if __name__ == "__main__":
77
- demo.launch()
 
2
  import gradio as gr
3
  import google.generativeai as genai
4
 
5
+ # Configure Gemini API (key must be set in Hugging Face Space secrets)
6
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
7
 
8
  # ---------- PROMPTS ----------
 
34
  6. Do not give marks outside the markscheme.
35
  Output must be a structured grading report with reasoning.
36
  """
 
 
 
 
 
 
37
 
38
+ # ---------- STEP 1: TRANSCRIPTION ----------
39
+ def transcribe(ans_file):
40
+ try:
41
+ ans_uploaded = genai.upload_file(path=ans_file.name, display_name="Answer Sheet")
42
 
43
  model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
44
+ resp = model.generate_content([TRANSCRIPTION_PROMPT, ans_uploaded])
45
+
46
+ transcription = getattr(resp, "text", None) or resp.candidates[0].content.parts[0].text
47
+ return transcription
48
+ except Exception as e:
49
+ return f"❌ Error during transcription: {e}"
50
 
51
+ # ---------- STEP 2: GRADING ----------
52
+ def grade(qp_file, ms_file, transcription):
53
+ try:
54
+ qp_uploaded = genai.upload_file(path=qp_file.name, display_name="Question Paper")
55
+ ms_uploaded = genai.upload_file(path=ms_file.name, display_name="Marking Scheme")
56
 
57
+ model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
58
+ resp = model.generate_content([GRADING_PROMPT, qp_uploaded, ms_uploaded, transcription])
59
 
60
+ grading = getattr(resp, "text", None) or resp.candidates[0].content.parts[0].text
61
+ return grading
62
  except Exception as e:
63
+ return f"❌ Error during grading: {e}"
64
+
65
+ # ---------- GRADIO APP ----------
66
+ with gr.Blocks(title="πŸ“˜ AI Teacher Assistant") as demo:
67
+ gr.Markdown("## πŸ“˜ AI Teacher Assistant\nUpload exam documents to transcribe and grade student answers step by step.")
68
+
69
+ with gr.Row():
70
+ qp_file = gr.File(label="Upload Question Paper (PDF)", type="filepath")
71
+ ms_file = gr.File(label="Upload Mark Scheme (PDF)", type="filepath")
72
+ ans_file = gr.File(label="Upload Student Answer Sheet (PDF)", type="filepath")
73
+
74
+ # Step 1: Transcription
75
+ transcribe_btn = gr.Button("Step 1: Transcribe Answer Sheet")
76
+ transcription_out = gr.Markdown(label="πŸ“„ Student Transcription")
77
+
78
+ # Step 2: Grading
79
+ grade_btn = gr.Button("Step 2: Grade the Student")
80
+ grading_out = gr.Textbox(label="βœ… Grading Report (Step-by-Step)", lines=20)
81
+
82
+ # Button Logic
83
+ transcribe_btn.click(fn=transcribe, inputs=[ans_file], outputs=[transcription_out])
84
+ grade_btn.click(fn=grade, inputs=[qp_file, ms_file, transcription_out], outputs=[grading_out])
85
 
86
  if __name__ == "__main__":
87
+ demo.launch()