atz21 commited on
Commit
ebf59a2
·
verified ·
1 Parent(s): 8ab3c83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -22
app.py CHANGED
@@ -13,20 +13,6 @@ genai.configure(api_key=GEMINI_API_KEY)
13
  # Initialize model
14
  model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
15
 
16
- # ---------- Helper: Safe Extraction ----------
17
- def safe_extract(response):
18
- """Safely get text from Gemini response"""
19
- try:
20
- if hasattr(response, "text") and response.text:
21
- return response.text
22
- elif response.candidates:
23
- for cand in response.candidates:
24
- if cand.content and cand.content.parts:
25
- return cand.content.parts[0].text
26
- return "⚠️ No response generated from Gemini."
27
- except Exception as e:
28
- return f"⚠️ Error extracting response: {str(e)}"
29
-
30
 
31
  # ---------- STEP 1: TRANSCRIPTION ----------
32
  def transcribe_files(qp_file, ms_file, ans_file):
@@ -39,7 +25,7 @@ def transcribe_files(qp_file, ms_file, ans_file):
39
  Persona:
40
  You are an expert transcriptionist specializing in scientific and mathematical documents.
41
  Your task is to transcribe the provided handwritten student solutions into Markdown with LaTeX.
42
-
43
  Rules:
44
  - Use LaTeX for all math ($ ... $ inline, $$ ... $$ for display).
45
  - Do NOT correct mistakes. Just transcribe faithfully.
@@ -47,13 +33,16 @@ def transcribe_files(qp_file, ms_file, ans_file):
47
  - Use **bold** for question numbering (e.g., **1.**, **2a.**).
48
  - Preserve step-by-step structure (use align* for multi-line equations).
49
  - If a symbol is illegible, mark it as [unclear].
50
-
51
- Output:
52
- - A single clean Markdown string with LaTeX equations.
53
  """
54
 
55
  response = model.generate_content([transcription_instructions, uploaded_as])
56
- return safe_extract(response)
 
 
 
 
 
 
57
 
58
 
59
  # ---------- STEP 2: GRADING ----------
@@ -77,7 +66,7 @@ def grade_files(qp_file, ms_file, ans_file, transcription):
77
  * Conceptual Error
78
  * Silly Mistake
79
  * None
80
-
81
  Output:
82
  - For each part: show awarded marks + justification.
83
  - Explain reasoning step by step.
@@ -90,7 +79,13 @@ def grade_files(qp_file, ms_file, ans_file, transcription):
90
  uploaded_ms,
91
  transcription
92
  ])
93
- return safe_extract(response)
 
 
 
 
 
 
94
 
95
 
96
  # ---------- GRADIO UI ----------
@@ -111,5 +106,4 @@ with gr.Blocks() as demo:
111
  transcribe_btn.click(fn=transcribe_files, inputs=[qp, ms, ans], outputs=transcription_output)
112
  grade_btn.click(fn=grade_files, inputs=[qp, ms, ans, transcription_output], outputs=grading_output)
113
 
114
- # Launch for Hugging Face
115
  demo.launch()
 
13
  # Initialize model
14
  model = genai.GenerativeModel("gemini-2.5-pro", generation_config={"temperature": 0})
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  # ---------- STEP 1: TRANSCRIPTION ----------
18
  def transcribe_files(qp_file, ms_file, ans_file):
 
25
  Persona:
26
  You are an expert transcriptionist specializing in scientific and mathematical documents.
27
  Your task is to transcribe the provided handwritten student solutions into Markdown with LaTeX.
28
+
29
  Rules:
30
  - Use LaTeX for all math ($ ... $ inline, $$ ... $$ for display).
31
  - Do NOT correct mistakes. Just transcribe faithfully.
 
33
  - Use **bold** for question numbering (e.g., **1.**, **2a.**).
34
  - Preserve step-by-step structure (use align* for multi-line equations).
35
  - If a symbol is illegible, mark it as [unclear].
 
 
 
36
  """
37
 
38
  response = model.generate_content([transcription_instructions, uploaded_as])
39
+
40
+ # Direct extraction (like in your notebook)
41
+ transcription = getattr(response, "text", None)
42
+ if not transcription:
43
+ transcription = response.candidates[0].content.parts[0].text
44
+
45
+ return transcription
46
 
47
 
48
  # ---------- STEP 2: GRADING ----------
 
66
  * Conceptual Error
67
  * Silly Mistake
68
  * None
69
+
70
  Output:
71
  - For each part: show awarded marks + justification.
72
  - Explain reasoning step by step.
 
79
  uploaded_ms,
80
  transcription
81
  ])
82
+
83
+ # Direct extraction (like in your notebook)
84
+ grading = getattr(response, "text", None)
85
+ if not grading:
86
+ grading = response.candidates[0].content.parts[0].text
87
+
88
+ return grading
89
 
90
 
91
  # ---------- GRADIO UI ----------
 
106
  transcribe_btn.click(fn=transcribe_files, inputs=[qp, ms, ans], outputs=transcription_output)
107
  grade_btn.click(fn=grade_files, inputs=[qp, ms, ans, transcription_output], outputs=grading_output)
108
 
 
109
  demo.launch()