Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# Split rubric into criteria
|
| 9 |
-
criteria = [criterion.strip() for criterion in
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 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 |
+
|