textevaluation / app.py
codeteach's picture
Update app.py
2068870 verified
raw
history blame
4.13 kB
import gradio as gr
import openai
import fitz # PyMuPDF
import time
# Set your OpenAI API key
openai.api_key = 'sk-proj-fCrObs9lnucfEFwJdMkHT3BlbkFJA7auo8szgjDuHz28QGBW'
def extract_text_from_pdf(pdf_file):
text = ""
try:
if isinstance(pdf_file, bytes):
document = fitz.open(stream=pdf_file, filetype="pdf")
else:
document = fitz.open(stream=pdf_file.read(), filetype="pdf")
for page_num in range(len(document)):
page = document.load_page(page_num)
text += page.get_text()
except Exception as e:
return str(e)
return text
def generate_summary(text):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a summarization assistant."},
{"role": "user", "content": f"Please summarize the following text: {text}"}
]
)
summary = response['choices'][0]['message']['content'].strip()
return summary
def evaluate_text_against_rubric(rubric_text, text):
start_time = time.time()
# Split rubric into criteria
criteria = [criterion.strip() for criterion in rubric_text.split('\n') if criterion.strip()]
if not criteria:
return "No valid criteria found in the rubric."
if not text:
return "No text provided for evaluation."
evaluations = {}
for i, criterion in enumerate(criteria):
try:
summary = generate_summary(text[:1000])
evaluations[f'Criteria {i+1}'] = {
"Criterion": criterion,
"Score": 3, # Dummy score for now
"Comment": f"Evaluation based on criterion: {criterion}",
"Example": summary
}
except Exception as e:
evaluations[f'Criteria {i+1}'] = {
"Criterion": criterion,
"Score": 0,
"Comment": f"Error during evaluation: {str(e)}",
"Example": ""
}
end_time = time.time()
print(f"Evaluation took {end_time - start_time} seconds")
return evaluations
def evaluate(rubric_pdf, rubric_text, text):
rubric = ""
if rubric_pdf is not None:
rubric = extract_text_from_pdf(rubric_pdf)
elif rubric_text:
rubric = rubric_text
if not rubric:
return "No rubric provided."
if not text:
return "No text provided for evaluation."
if len(text) > 2000:
return "The text provided exceeds the 2000 character limit."
evaluation = evaluate_text_against_rubric(rubric, text)
if isinstance(evaluation, str): # If it's an error message
return evaluation
evaluation_text = ""
for criterion, details in evaluation.items():
evaluation_text += f"{criterion}:\n"
evaluation_text += f" Criterion: {details['Criterion']}\n"
evaluation_text += f" Score: {details['Score']}\n"
evaluation_text += f" Comment: {details['Comment']}\n"
evaluation_text += f" Example: {details['Example']}\n\n"
return evaluation_text
# Create Gradio interface
with gr.Blocks() as interface:
gr.Markdown("# PDF Text Evaluator")
gr.Markdown("Upload a rubric as a PDF or paste the rubric text, then paste text for evaluation.")
rubric_pdf_input = gr.File(label="Upload Rubric PDF (optional)", type="binary")
rubric_text_input = gr.Textbox(lines=10, placeholder="Or enter your rubric text here...", label="Rubric Text (optional)")
text_input = gr.Textbox(lines=10, placeholder="Paste the text to be evaluated here (max 2000 characters)...", label="Text to Evaluate")
evaluate_button = gr.Button("Evaluate")
output = gr.Textbox(label="Evaluation Results")
def evaluate_button_clicked(rubric_pdf, rubric_text, text):
return evaluate(rubric_pdf, rubric_text, text)
evaluate_button.click(evaluate_button_clicked, inputs=[rubric_pdf_input, rubric_text_input, text_input], outputs=output)
# Launch the interface
interface.launch()