jeremierostan commited on
Commit
548b5a9
1 Parent(s): c6e0d4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -51
app.py CHANGED
@@ -9,11 +9,12 @@ from groq import Groq
9
  groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
10
 
11
  # PDF Extraction Function
12
- def extract_text_from_pdf(pdf_file):
13
- reader = PyPDF2.PdfReader(pdf_file)
14
- extracted_text = ""
15
- for page in reader.pages:
16
- extracted_text += page.extract_text()
 
17
  return extracted_text
18
 
19
  # Function to generate a question using Groq (Improved Prompt)
@@ -30,7 +31,7 @@ def generate_question(paper_text):
30
  messages=[{"role": "user", "content": prompt}],
31
  model="llama-3.1-70b-versatile"
32
  )
33
- # Access the message content properly, no subscripting
34
  return response.choices[0].message.content
35
 
36
  # Validate the student's answer using Groq (Improved Prompt)
@@ -50,42 +51,23 @@ def validate_answer(paper_text, student_answer):
50
  messages=[{"role": "user", "content": prompt}],
51
  model="llama-3.1-70b-versatile"
52
  )
53
- # Access the message content properly, no subscripting
54
  return response.choices[0].message.content
55
 
56
- # Function to determine authorship based on multiple interactions
57
- def determine_authorship(paper_text, student_answers, interaction_count):
58
- # Accumulate evidence based on the student's answers over multiple rounds
59
- prompt = (
60
- f"Based on the student’s answers to several questions, evaluate whether the student is likely the author of the paper. "
61
- f"Consider the depth of understanding reflected in their responses, their ability to explain concepts in the paper, and "
62
- f"whether their explanations match the complexity of the original text. If their answers show gaps in understanding or seem "
63
- f"inconsistent with the paper's content, note these concerns. If after {interaction_count} interactions, the student consistently "
64
- f"shows a deep understanding, confirm likely authorship. Otherwise, suggest that authorship is uncertain.\n\n"
65
- f"Paper text: '{paper_text}'\n"
66
- f"Student’s responses: '{student_answers}'"
67
- )
68
-
69
- response = groq_client.chat.completions.create(
70
- messages=[{"role": "user", "content": prompt}],
71
- model="llama-3.1-70b-versatile"
72
- )
73
- # Access the message content properly, no subscripting
74
- return response.choices[0].message.content
75
-
76
- # Mailjet Email Sending Function
77
- def send_email(pdf_file, student_answer, verification_status):
78
  api_key = os.getenv("MAILJET_API_KEY")
79
  api_secret = os.getenv("MAILJET_SECRET_KEY")
80
  mailjet = Client(auth=(api_key, api_secret), version='v3.1')
81
 
 
 
 
 
82
  # Determine the email subject and content based on verification status
83
  subject = "Authorship Confirmation" if verification_status == "Confirmed" else "Authorship Concerns"
84
  message_content = f"Authorship Status: {verification_status}\n\nStudent Answer: {student_answer}"
85
 
86
- # Attach the original PDF in the email
87
- attachment_content = base64.b64encode(pdf_file.read()).decode('utf-8')
88
-
89
  data = {
90
  'Messages': [
91
  {
@@ -104,7 +86,7 @@ def send_email(pdf_file, student_answer, verification_status):
104
  "Attachments": [
105
  {
106
  "ContentType": "application/pdf",
107
- "Filename": pdf_file.name,
108
  "Base64Content": attachment_content
109
  }
110
  ]
@@ -115,38 +97,50 @@ def send_email(pdf_file, student_answer, verification_status):
115
  return result.status_code
116
 
117
  # Gradio Interface for the main app
118
- def main_app(pdf_file, student_answer, interaction_count=3):
119
- # Extract text from the PDF
120
- paper_text = extract_text_from_pdf(pdf_file)
 
121
 
122
- # Generate a list of questions to challenge the student's understanding
123
- student_answers = []
124
- for i in range(interaction_count):
125
- question = generate_question(paper_text)
126
- student_answers.append(f"Q{i+1}: {question}\nA{i+1}: {student_answer}")
127
 
128
- # Validate the student's answer for each question
129
- final_verification = determine_authorship(paper_text, student_answers, interaction_count)
 
 
 
 
 
 
 
 
130
 
131
- # Send an email based on the final verification status
132
- verification_status = "Confirmed" if "likely the author" in final_verification else "Not Confirmed"
133
- send_email(pdf_file, student_answer, verification_status)
134
 
135
- return f"Final Verification: {final_verification}\nStatus: {verification_status}"
 
 
 
136
 
137
  # Gradio Blocks for the App
138
  with gr.Blocks() as app:
139
  gr.Markdown("# Authorship Verification App")
140
 
141
  pdf_file = gr.File(label="Upload PDF Paper")
 
142
  student_answer = gr.Textbox(label="Student Answer", placeholder="Answer the question here...")
143
 
 
144
  submit_btn = gr.Button("Submit Answer")
145
- output = gr.Textbox(label="Result")
 
 
 
146
 
147
- # Ensure that the file upload triggers the processing and the student can submit their answer
148
- pdf_file.upload(fn=extract_text_from_pdf, inputs=pdf_file, outputs=None)
149
- submit_btn.click(fn=main_app, inputs=[pdf_file, student_answer], outputs=output)
150
 
151
  # Launch the app
152
  app.launch()
 
9
  groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
10
 
11
  # PDF Extraction Function
12
+ def extract_text_from_pdf(pdf_file_path):
13
+ with open(pdf_file_path, 'rb') as file:
14
+ reader = PyPDF2.PdfReader(file)
15
+ extracted_text = ""
16
+ for page in reader.pages:
17
+ extracted_text += page.extract_text()
18
  return extracted_text
19
 
20
  # Function to generate a question using Groq (Improved Prompt)
 
31
  messages=[{"role": "user", "content": prompt}],
32
  model="llama-3.1-70b-versatile"
33
  )
34
+ # Access the message content properly
35
  return response.choices[0].message.content
36
 
37
  # Validate the student's answer using Groq (Improved Prompt)
 
51
  messages=[{"role": "user", "content": prompt}],
52
  model="llama-3.1-70b-versatile"
53
  )
54
+ # Access the message content properly
55
  return response.choices[0].message.content
56
 
57
+ # Mailjet Email Sending Function (Corrected)
58
+ def send_email(pdf_file_path, student_answer, verification_status):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  api_key = os.getenv("MAILJET_API_KEY")
60
  api_secret = os.getenv("MAILJET_SECRET_KEY")
61
  mailjet = Client(auth=(api_key, api_secret), version='v3.1')
62
 
63
+ # Open and read the PDF file as binary content
64
+ with open(pdf_file_path, "rb") as file:
65
+ attachment_content = base64.b64encode(file.read()).decode('utf-8')
66
+
67
  # Determine the email subject and content based on verification status
68
  subject = "Authorship Confirmation" if verification_status == "Confirmed" else "Authorship Concerns"
69
  message_content = f"Authorship Status: {verification_status}\n\nStudent Answer: {student_answer}"
70
 
 
 
 
71
  data = {
72
  'Messages': [
73
  {
 
86
  "Attachments": [
87
  {
88
  "ContentType": "application/pdf",
89
+ "Filename": os.path.basename(pdf_file_path),
90
  "Base64Content": attachment_content
91
  }
92
  ]
 
97
  return result.status_code
98
 
99
  # Gradio Interface for the main app
100
+ def handle_pdf_upload(pdf_file):
101
+ # Save the uploaded file and extract text from it
102
+ pdf_file_path = pdf_file.name
103
+ paper_text = extract_text_from_pdf(pdf_file_path)
104
 
105
+ # Generate the first question based on the paper's content
106
+ question = generate_question(paper_text)
 
 
 
107
 
108
+ # Return the generated question for display to the user
109
+ return question
110
+
111
+ def handle_answer_submission(pdf_file, student_answer):
112
+ # Extract the paper text for validation
113
+ pdf_file_path = pdf_file.name
114
+ paper_text = extract_text_from_pdf(pdf_file_path)
115
+
116
+ # Validate the student's answer
117
+ validation_result = validate_answer(paper_text, student_answer)
118
 
119
+ # Determine verification status based on the validation result
120
+ verification_status = "Confirmed" if "likely understanding" in validation_result else "Not Confirmed"
 
121
 
122
+ # Send an email to the teacher with the result and the PDF attachment
123
+ send_email(pdf_file_path, student_answer, verification_status)
124
+
125
+ return f"Final Verification: {validation_result}\nStatus: {verification_status}"
126
 
127
  # Gradio Blocks for the App
128
  with gr.Blocks() as app:
129
  gr.Markdown("# Authorship Verification App")
130
 
131
  pdf_file = gr.File(label="Upload PDF Paper")
132
+ question_display = gr.Textbox(label="Generated Question", interactive=False)
133
  student_answer = gr.Textbox(label="Student Answer", placeholder="Answer the question here...")
134
 
135
+ upload_btn = gr.Button("Upload and Generate Question")
136
  submit_btn = gr.Button("Submit Answer")
137
+ output = gr.Textbox(label="Result", interactive=False)
138
+
139
+ # Upon PDF upload, generate and display the question
140
+ upload_btn.click(fn=handle_pdf_upload, inputs=pdf_file, outputs=question_display)
141
 
142
+ # Upon answer submission, validate the answer and return the result
143
+ submit_btn.click(fn=handle_answer_submission, inputs=[pdf_file, student_answer], outputs=output)
 
144
 
145
  # Launch the app
146
  app.launch()