codeteach commited on
Commit
fb00050
·
verified ·
1 Parent(s): 20e8be4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -13
app.py CHANGED
@@ -1,12 +1,24 @@
1
  import gradio as gr
2
  from transformers import pipeline, PipelineException
 
3
 
4
  # Load a summarization model from Hugging Face
5
  summarizer = pipeline("summarization")
6
 
7
- def evaluate_text_against_rubric(rubric, text):
 
 
 
 
 
 
 
 
 
 
 
8
  # Split rubric into criteria
9
- criteria = [criterion.strip() for criterion in rubric.split('\n') if criterion.strip()]
10
 
11
  if not criteria:
12
  return "No valid criteria found in the rubric."
@@ -41,7 +53,19 @@ def evaluate_text_against_rubric(rubric, text):
41
 
42
  return evaluations
43
 
44
- def evaluate(rubric, text):
 
 
 
 
 
 
 
 
 
 
 
 
45
  evaluation = evaluate_text_against_rubric(rubric, text)
46
 
47
  if isinstance(evaluation, str): # If it's an error message
@@ -58,17 +82,24 @@ def evaluate(rubric, text):
58
  return evaluation_text
59
 
60
  # Create Gradio interface
61
- interface = gr.Interface(
62
- fn=evaluate,
63
- inputs=[
64
- gr.Textbox(lines=10, placeholder="Enter your rubric here..."),
65
- gr.Textbox(lines=10, placeholder="Paste the text to be evaluated here...")
66
- ],
67
- outputs="text",
68
- title="Text Evaluator",
69
- description="Upload or enter a rubric and paste text for evaluation. The system will grade the text against the rubric and provide feedback."
70
- )
 
 
 
 
 
 
71
 
72
  # Launch the interface
73
  interface.launch()
74
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline, PipelineException
3
+ import fitz # PyMuPDF
4
 
5
  # Load a summarization model from Hugging Face
6
  summarizer = pipeline("summarization")
7
 
8
+ def extract_text_from_pdf(pdf_file):
9
+ text = ""
10
+ try:
11
+ document = fitz.open(stream=pdf_file.read(), filetype="pdf")
12
+ for page_num in range(len(document)):
13
+ page = document.load_page(page_num)
14
+ text += page.get_text()
15
+ except Exception as e:
16
+ return str(e)
17
+ return text
18
+
19
+ def evaluate_text_against_rubric(rubric_text, text):
20
  # Split rubric into criteria
21
+ criteria = [criterion.strip() for criterion in rubric_text.split('\n') if criterion.strip()]
22
 
23
  if not criteria:
24
  return "No valid criteria found in the rubric."
 
53
 
54
  return evaluations
55
 
56
+ def evaluate(rubric_pdf, rubric_text, text):
57
+ rubric = ""
58
+ if rubric_pdf is not None:
59
+ rubric = extract_text_from_pdf(rubric_pdf)
60
+ elif rubric_text:
61
+ rubric = rubric_text
62
+
63
+ if not rubric:
64
+ return "No rubric provided."
65
+
66
+ if not text:
67
+ return "No text provided for evaluation."
68
+
69
  evaluation = evaluate_text_against_rubric(rubric, text)
70
 
71
  if isinstance(evaluation, str): # If it's an error message
 
82
  return evaluation_text
83
 
84
  # Create Gradio interface
85
+ with gr.Blocks() as interface:
86
+ gr.Markdown("# PDF Text Evaluator")
87
+ gr.Markdown("Upload a rubric as a PDF or paste the rubric text, then paste text for evaluation.")
88
+
89
+ rubric_pdf_input = gr.File(label="Upload Rubric PDF (optional)", type="file")
90
+ rubric_text_input = gr.Textbox(lines=10, placeholder="Or enter your rubric text here...", label="Rubric Text (optional)")
91
+ text_input = gr.Textbox(lines=10, placeholder="Paste the text to be evaluated here...", label="Text to Evaluate")
92
+
93
+ evaluate_button = gr.Button("Evaluate")
94
+
95
+ output = gr.Textbox(label="Evaluation Results")
96
+
97
+ def evaluate_button_clicked(rubric_pdf, rubric_text, text):
98
+ return evaluate(rubric_pdf, rubric_text, text)
99
+
100
+ evaluate_button.click(evaluate_button_clicked, inputs=[rubric_pdf_input, rubric_text_input, text_input], outputs=output)
101
 
102
  # Launch the interface
103
  interface.launch()
104
 
105
+