import gradio as gr from transformers import pipeline, PipelineException # Load a summarization model from Hugging Face summarizer = pipeline("summarization") def evaluate_text_against_rubric(rubric, text): # Split rubric into criteria criteria = [criterion.strip() for criterion in rubric.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 = summarizer(text, max_length=50, min_length=25, do_sample=False)[0]['summary_text'] evaluations[f'Criteria {i+1}'] = { "Criterion": criterion, "Score": 3, # Dummy score for now "Comment": f"Evaluation based on criterion: {criterion}", "Example": summary } except PipelineException as e: evaluations[f'Criteria {i+1}'] = { "Criterion": criterion, "Score": 0, "Comment": f"Error during evaluation: {str(e)}", "Example": "" } except Exception as e: evaluations[f'Criteria {i+1}'] = { "Criterion": criterion, "Score": 0, "Comment": f"An unexpected error occurred: {str(e)}", "Example": "" } return evaluations def evaluate(rubric, text): 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 interface = gr.Interface( fn=evaluate, inputs=[ gr.Textbox(lines=10, placeholder="Enter your rubric here..."), gr.Textbox(lines=10, placeholder="Paste the text to be evaluated here...") ], outputs="text", title="Text Evaluator", description="Upload or enter a rubric and paste text for evaluation. The system will grade the text against the rubric and provide feedback." ) # Launch the interface interface.launch()