jeremierostan commited on
Commit
a1f9fa5
1 Parent(s): b9eb937

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -13
app.py CHANGED
@@ -1,11 +1,7 @@
1
- import os
2
  import PyPDF2
3
  import gradio as gr
4
  from groq import Groq
5
 
6
- # Initialize Groq client
7
- groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
8
-
9
  # Function to extract PDF
10
  def extract_text_from_pdf(pdf_file_path):
11
  with open(pdf_file_path, 'rb') as file:
@@ -16,7 +12,8 @@ def extract_text_from_pdf(pdf_file_path):
16
  return extracted_text
17
 
18
  # Function to generate different types of questions using Groq
19
- def generate_questions(paper_text):
 
20
  prompts = [
21
  f"Read this paper submitted by a student. Then, ask them to briefly summarize its outline. Ask the question directly. Do not comment on the paper or add anything else. Present this as a reflection exercise. Paper text: '{paper_text}'",
22
  f"Read this paper submitted by a student. Identify a key term that is central to the discussion. Then, ask the student to explain it in context (provide an excerpt from the paper). Do not comment on the paper or add anything else. Paper text: '{paper_text}'",
@@ -35,7 +32,8 @@ def generate_questions(paper_text):
35
  return questions
36
 
37
  # Function to validate each student's answer
38
- def validate_answer(paper_text, question, student_answer):
 
39
  prompt = (
40
  f"The student was asked questions about a paper they submitted. Compare their answers to the content of the paper."
41
  f"Determine if the student's answer reflects an understanding of the paper's content. The goal is to determine whether there are reasons to believe or to doubt that they understand its content, and therefore that they are its author."
@@ -50,19 +48,19 @@ def validate_answer(paper_text, question, student_answer):
50
  return response.choices[0].message.content
51
 
52
  # Function to handle PDF upload and question generation
53
- def handle_pdf_upload(pdf_file):
54
  # Save the uploaded file and extract text from it
55
  pdf_file_path = pdf_file.name
56
  paper_text = extract_text_from_pdf(pdf_file_path)
57
 
58
  # Generate four types of questions based on the paper's content
59
- questions = generate_questions(paper_text)
60
 
61
  # Return the generated questions for display
62
  return questions
63
 
64
  # Function to handle answer submission and LLM's final judgment
65
- def handle_answer_submission(pdf_file, answer1, answer2, answer3, answer4):
66
  # Extract the paper text for validation
67
  pdf_file_path = pdf_file.name
68
  paper_text = extract_text_from_pdf(pdf_file_path)
@@ -71,14 +69,14 @@ def handle_answer_submission(pdf_file, answer1, answer2, answer3, answer4):
71
  answers_summary = ""
72
 
73
  # Loop over each question-answer pair and validate
74
- questions = generate_questions(paper_text)
75
  verification_status = "Thank you! "
76
  emoji = "🟢"
77
 
78
  answers = [answer1, answer2, answer3, answer4]
79
 
80
  for i in range(len(questions)):
81
- validation_result = validate_answer(paper_text, questions[i], answers[i])
82
  answers_summary += f"Question {i+1}: {questions[i]}\nAnswer: {answers[i]}\nValidation: {validation_result}\n\n"
83
  if "Flagged" in validation_result:
84
  verification_status = "Please talk to your teacher about this submission 🟠"
@@ -93,6 +91,9 @@ with gr.Blocks() as app:
93
  logo_url = "https://i.ibb.co/S7DCk3K/a-logo-design-with-a-stylized-egyptian-scribe-the-i-L59-Gh-Zn-QASp7v-TUazjdp-A-1-s-Ip-Qq-VRIif-VF5dn.png" # Replace with your actual logo URL
94
  gr.Image(logo_url, label="", show_label=False, height=200)
95
 
 
 
 
96
  # PDF upload Section
97
  pdf_file = gr.File(label="Upload PDF Paper")
98
  upload_btn = gr.Button("Upload and Generate Questions")
@@ -114,10 +115,10 @@ with gr.Blocks() as app:
114
  output = gr.Textbox(label="Result", interactive=False)
115
 
116
  # Layout: Upload button right under the file upload, followed by Q/A section
117
- upload_btn.click(fn=handle_pdf_upload, inputs=pdf_file, outputs=[question1, question2, question3, question4])
118
  submit_btn = gr.Button("Submit Answers")
119
  # Passing answers individually
120
- submit_btn.click(fn=handle_answer_submission, inputs=[pdf_file, answer1, answer2, answer3, answer4], outputs=output)
121
 
122
  # "Powered by Groq" badge
123
  with gr.Row():
 
 
1
  import PyPDF2
2
  import gradio as gr
3
  from groq import Groq
4
 
 
 
 
5
  # Function to extract PDF
6
  def extract_text_from_pdf(pdf_file_path):
7
  with open(pdf_file_path, 'rb') as file:
 
12
  return extracted_text
13
 
14
  # Function to generate different types of questions using Groq
15
+ def generate_questions(api_key, paper_text):
16
+ groq_client = Groq(api_key=api_key) # Use the provided API key
17
  prompts = [
18
  f"Read this paper submitted by a student. Then, ask them to briefly summarize its outline. Ask the question directly. Do not comment on the paper or add anything else. Present this as a reflection exercise. Paper text: '{paper_text}'",
19
  f"Read this paper submitted by a student. Identify a key term that is central to the discussion. Then, ask the student to explain it in context (provide an excerpt from the paper). Do not comment on the paper or add anything else. Paper text: '{paper_text}'",
 
32
  return questions
33
 
34
  # Function to validate each student's answer
35
+ def validate_answer(api_key, paper_text, question, student_answer):
36
+ groq_client = Groq(api_key=api_key) # Use the provided API key
37
  prompt = (
38
  f"The student was asked questions about a paper they submitted. Compare their answers to the content of the paper."
39
  f"Determine if the student's answer reflects an understanding of the paper's content. The goal is to determine whether there are reasons to believe or to doubt that they understand its content, and therefore that they are its author."
 
48
  return response.choices[0].message.content
49
 
50
  # Function to handle PDF upload and question generation
51
+ def handle_pdf_upload(api_key, pdf_file):
52
  # Save the uploaded file and extract text from it
53
  pdf_file_path = pdf_file.name
54
  paper_text = extract_text_from_pdf(pdf_file_path)
55
 
56
  # Generate four types of questions based on the paper's content
57
+ questions = generate_questions(api_key, paper_text)
58
 
59
  # Return the generated questions for display
60
  return questions
61
 
62
  # Function to handle answer submission and LLM's final judgment
63
+ def handle_answer_submission(api_key, pdf_file, answer1, answer2, answer3, answer4):
64
  # Extract the paper text for validation
65
  pdf_file_path = pdf_file.name
66
  paper_text = extract_text_from_pdf(pdf_file_path)
 
69
  answers_summary = ""
70
 
71
  # Loop over each question-answer pair and validate
72
+ questions = generate_questions(api_key, paper_text)
73
  verification_status = "Thank you! "
74
  emoji = "🟢"
75
 
76
  answers = [answer1, answer2, answer3, answer4]
77
 
78
  for i in range(len(questions)):
79
+ validation_result = validate_answer(api_key, paper_text, questions[i], answers[i])
80
  answers_summary += f"Question {i+1}: {questions[i]}\nAnswer: {answers[i]}\nValidation: {validation_result}\n\n"
81
  if "Flagged" in validation_result:
82
  verification_status = "Please talk to your teacher about this submission 🟠"
 
91
  logo_url = "https://i.ibb.co/S7DCk3K/a-logo-design-with-a-stylized-egyptian-scribe-the-i-L59-Gh-Zn-QASp7v-TUazjdp-A-1-s-Ip-Qq-VRIif-VF5dn.png" # Replace with your actual logo URL
92
  gr.Image(logo_url, label="", show_label=False, height=200)
93
 
94
+ # API key input Section
95
+ api_key_input = gr.Textbox(label="Enter your Groq API Key", placeholder="You can create a free key at https://console.groq.com/keys")
96
+
97
  # PDF upload Section
98
  pdf_file = gr.File(label="Upload PDF Paper")
99
  upload_btn = gr.Button("Upload and Generate Questions")
 
115
  output = gr.Textbox(label="Result", interactive=False)
116
 
117
  # Layout: Upload button right under the file upload, followed by Q/A section
118
+ upload_btn.click(fn=handle_pdf_upload, inputs=[api_key_input, pdf_file], outputs=[question1, question2, question3, question4])
119
  submit_btn = gr.Button("Submit Answers")
120
  # Passing answers individually
121
+ submit_btn.click(fn=handle_answer_submission, inputs=[api_key_input, pdf_file, answer1, answer2, answer3, answer4], outputs=output)
122
 
123
  # "Powered by Groq" badge
124
  with gr.Row():