Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,22 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 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 = rubric.split('\n')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# Use summarizer for each criterion as a dummy placeholder
|
| 12 |
evaluations = {}
|
| 13 |
for i, criterion in enumerate(criteria):
|
| 14 |
-
|
| 15 |
summary = summarizer(text, max_length=50, min_length=25, do_sample=False)[0]['summary_text']
|
| 16 |
evaluations[f'Criteria {i+1}'] = {
|
| 17 |
"Criterion": criterion,
|
|
@@ -19,10 +24,29 @@ def evaluate_text_against_rubric(rubric, text):
|
|
| 19 |
"Comment": f"Evaluation based on criterion: {criterion}",
|
| 20 |
"Example": summary
|
| 21 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return evaluations
|
| 23 |
|
| 24 |
def evaluate(rubric, text):
|
| 25 |
evaluation = evaluate_text_against_rubric(rubric, text)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
evaluation_text = ""
|
| 27 |
for criterion, details in evaluation.items():
|
| 28 |
evaluation_text += f"{criterion}:\n"
|
|
@@ -30,6 +54,7 @@ def evaluate(rubric, text):
|
|
| 30 |
evaluation_text += f" Score: {details['Score']}\n"
|
| 31 |
evaluation_text += f" Comment: {details['Comment']}\n"
|
| 32 |
evaluation_text += f" Example: {details['Example']}\n\n"
|
|
|
|
| 33 |
return evaluation_text
|
| 34 |
|
| 35 |
# Create Gradio interface
|
|
@@ -46,3 +71,4 @@ interface = gr.Interface(
|
|
| 46 |
|
| 47 |
# Launch the interface
|
| 48 |
interface.launch()
|
|
|
|
|
|
| 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."
|
| 13 |
+
|
| 14 |
+
if not text:
|
| 15 |
+
return "No text provided for evaluation."
|
| 16 |
|
|
|
|
| 17 |
evaluations = {}
|
| 18 |
for i, criterion in enumerate(criteria):
|
| 19 |
+
try:
|
| 20 |
summary = summarizer(text, max_length=50, min_length=25, do_sample=False)[0]['summary_text']
|
| 21 |
evaluations[f'Criteria {i+1}'] = {
|
| 22 |
"Criterion": criterion,
|
|
|
|
| 24 |
"Comment": f"Evaluation based on criterion: {criterion}",
|
| 25 |
"Example": summary
|
| 26 |
}
|
| 27 |
+
except PipelineException as e:
|
| 28 |
+
evaluations[f'Criteria {i+1}'] = {
|
| 29 |
+
"Criterion": criterion,
|
| 30 |
+
"Score": 0,
|
| 31 |
+
"Comment": f"Error during evaluation: {str(e)}",
|
| 32 |
+
"Example": ""
|
| 33 |
+
}
|
| 34 |
+
except Exception as e:
|
| 35 |
+
evaluations[f'Criteria {i+1}'] = {
|
| 36 |
+
"Criterion": criterion,
|
| 37 |
+
"Score": 0,
|
| 38 |
+
"Comment": f"An unexpected error occurred: {str(e)}",
|
| 39 |
+
"Example": ""
|
| 40 |
+
}
|
| 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
|
| 48 |
+
return evaluation
|
| 49 |
+
|
| 50 |
evaluation_text = ""
|
| 51 |
for criterion, details in evaluation.items():
|
| 52 |
evaluation_text += f"{criterion}:\n"
|
|
|
|
| 54 |
evaluation_text += f" Score: {details['Score']}\n"
|
| 55 |
evaluation_text += f" Comment: {details['Comment']}\n"
|
| 56 |
evaluation_text += f" Example: {details['Example']}\n\n"
|
| 57 |
+
|
| 58 |
return evaluation_text
|
| 59 |
|
| 60 |
# Create Gradio interface
|
|
|
|
| 71 |
|
| 72 |
# Launch the interface
|
| 73 |
interface.launch()
|
| 74 |
+
|