atz21 commited on
Commit
af77078
·
verified ·
1 Parent(s): d5fff10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -14
app.py CHANGED
@@ -4,7 +4,6 @@ import google.generativeai as genai
4
  from reportlab.platypus import SimpleDocTemplate, Paragraph
5
  from reportlab.lib.styles import getSampleStyleSheet
6
  from reportlab.lib.pagesizes import A4
7
- from google.generativeai.types import HarmCategory, HarmBlockThreshold, SafetySetting
8
 
9
  # -------------------- CONFIG --------------------
10
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
@@ -76,6 +75,14 @@ def safe_generate(model, inputs, fallback_prompt=None):
76
  except Exception as e:
77
  return None, f"❌ Exception: {e}"
78
 
 
 
 
 
 
 
 
 
79
  # ---------- STEP 1: TRANSCRIPTION ----------
80
  def transcribe(ans_file):
81
  try:
@@ -83,12 +90,7 @@ def transcribe(ans_file):
83
  model = genai.GenerativeModel(
84
  "gemini-2.5-pro",
85
  generation_config={"temperature": 0},
86
- safety_settings=[
87
- SafetySetting(category=HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold=HarmBlockThreshold.BLOCK_NONE),
88
- SafetySetting(category=HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, threshold=HarmBlockThreshold.BLOCK_NONE),
89
- SafetySetting(category=HarmCategory.HARM_CATEGORY_HATE_SPEECH, threshold=HarmBlockThreshold.BLOCK_NONE),
90
- SafetySetting(category=HarmCategory.HARM_CATEGORY_HARASSMENT, threshold=HarmBlockThreshold.BLOCK_NONE),
91
- ]
92
  )
93
 
94
  transcription, error = safe_generate(model, [TRANSCRIPTION_PROMPT, ans_uploaded], fallback_prompt="Convert the PDF into structured plain text with questions separated.")
@@ -108,12 +110,7 @@ def grade(qp_file, ms_file, transcription):
108
  model = genai.GenerativeModel(
109
  "gemini-2.5-pro",
110
  generation_config={"temperature": 0},
111
- safety_settings=[
112
- SafetySetting(category=HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold=HarmBlockThreshold.BLOCK_NONE),
113
- SafetySetting(category=HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, threshold=HarmBlockThreshold.BLOCK_NONE),
114
- SafetySetting(category=HarmCategory.HARM_CATEGORY_HATE_SPEECH, threshold=HarmBlockThreshold.BLOCK_NONE),
115
- SafetySetting(category=HarmCategory.HARM_CATEGORY_HARASSMENT, threshold=HarmBlockThreshold.BLOCK_NONE),
116
- ]
117
  )
118
 
119
  grading, error = safe_generate(model, [GRADING_PROMPT, qp_uploaded, ms_uploaded, transcription], fallback_prompt="Grade the answers according to the marking scheme. Show marks step by step.")
@@ -161,4 +158,4 @@ with gr.Blocks(title="LeadIB AI Grading") as demo:
161
  )
162
 
163
  if __name__ == "__main__":
164
- demo.launch()
 
4
  from reportlab.platypus import SimpleDocTemplate, Paragraph
5
  from reportlab.lib.styles import getSampleStyleSheet
6
  from reportlab.lib.pagesizes import A4
 
7
 
8
  # -------------------- CONFIG --------------------
9
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
 
75
  except Exception as e:
76
  return None, f"❌ Exception: {e}"
77
 
78
+ # ---------- COMMON SAFETY SETTINGS ----------
79
+ safety_settings = [
80
+ {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
81
+ {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
82
+ {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
83
+ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
84
+ ]
85
+
86
  # ---------- STEP 1: TRANSCRIPTION ----------
87
  def transcribe(ans_file):
88
  try:
 
90
  model = genai.GenerativeModel(
91
  "gemini-2.5-pro",
92
  generation_config={"temperature": 0},
93
+ safety_settings=safety_settings
 
 
 
 
 
94
  )
95
 
96
  transcription, error = safe_generate(model, [TRANSCRIPTION_PROMPT, ans_uploaded], fallback_prompt="Convert the PDF into structured plain text with questions separated.")
 
110
  model = genai.GenerativeModel(
111
  "gemini-2.5-pro",
112
  generation_config={"temperature": 0},
113
+ safety_settings=safety_settings
 
 
 
 
 
114
  )
115
 
116
  grading, error = safe_generate(model, [GRADING_PROMPT, qp_uploaded, ms_uploaded, transcription], fallback_prompt="Grade the answers according to the marking scheme. Show marks step by step.")
 
158
  )
159
 
160
  if __name__ == "__main__":
161
+ demo.launch()