Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
def plot_likert_scores(file):
|
| 7 |
+
# Load the Excel file into pandas
|
| 8 |
+
df = pd.read_excel(file.name, sheet_name=0, header=None)
|
| 9 |
+
|
| 10 |
+
# Extract pre and post survey blocks (edit row indices if needed)
|
| 11 |
+
pre_df = df.iloc[1:5, :6].copy()
|
| 12 |
+
post_df = df.iloc[12:15, :6].copy()
|
| 13 |
+
|
| 14 |
+
pre_df.columns = ['identifier', 'Degree', 'Confidence', 'Feedback', 'Preparedness', 'Enjoyment']
|
| 15 |
+
post_df.columns = ['identifier', 'Degree', 'Confidence', 'Feedback', 'Preparedness', 'Enjoyment']
|
| 16 |
+
|
| 17 |
+
# Clean identifier
|
| 18 |
+
def clean_identifier(x):
|
| 19 |
+
try:
|
| 20 |
+
return int(str(x).strip())
|
| 21 |
+
except:
|
| 22 |
+
return np.nan
|
| 23 |
+
|
| 24 |
+
pre_df['identifier'] = pre_df['identifier'].apply(clean_identifier)
|
| 25 |
+
post_df['identifier'] = post_df['identifier'].apply(clean_identifier)
|
| 26 |
+
pre_df = pre_df.dropna(subset=['identifier'])
|
| 27 |
+
post_df = post_df.dropna(subset=['identifier'])
|
| 28 |
+
pre_df['identifier'] = pre_df['identifier'].astype(int)
|
| 29 |
+
post_df['identifier'] = post_df['identifier'].astype(int)
|
| 30 |
+
|
| 31 |
+
# Filter for identifier 7
|
| 32 |
+
pre_7 = pre_df[pre_df['identifier'] == 7]
|
| 33 |
+
post_7 = post_df[post_df['identifier'] == 7]
|
| 34 |
+
|
| 35 |
+
# If data for identifier 7 is missing
|
| 36 |
+
if pre_7.empty or post_7.empty:
|
| 37 |
+
return "Identifier 7 is missing from pre or post survey data. Please check your file."
|
| 38 |
+
|
| 39 |
+
# Map Likert values
|
| 40 |
+
likert_map = {
|
| 41 |
+
'Strongly Disagree': 1,
|
| 42 |
+
'Disagree': 2,
|
| 43 |
+
'Agree': 3,
|
| 44 |
+
'Strongly Agree': 4
|
| 45 |
+
}
|
| 46 |
+
questions = ['Confidence', 'Feedback', 'Enjoyment']
|
| 47 |
+
pretty_labels = ['Confidence', 'Feedback', 'Enjoyment']
|
| 48 |
+
|
| 49 |
+
pre_scores = [likert_map.get(pre_7[q].values[0], np.nan) for q in questions]
|
| 50 |
+
post_scores = [likert_map.get(post_7[q].values[0], np.nan) for q in questions]
|
| 51 |
+
|
| 52 |
+
# Plot
|
| 53 |
+
x = np.arange(len(questions))
|
| 54 |
+
width = 0.35
|
| 55 |
+
|
| 56 |
+
fig, ax = plt.subplots(figsize=(7, 5))
|
| 57 |
+
bars1 = ax.bar(x - width/2, pre_scores, width, label='Pre-survey', color='#3182bd', edgecolor='black')
|
| 58 |
+
bars2 = ax.bar(x + width/2, post_scores, width, label='Post-survey', color='#fdae6b', edgecolor='black')
|
| 59 |
+
|
| 60 |
+
# Add value labels
|
| 61 |
+
for bar in bars1 + bars2:
|
| 62 |
+
height = bar.get_height()
|
| 63 |
+
if not np.isnan(height):
|
| 64 |
+
ax.annotate(f'{int(height)}',
|
| 65 |
+
xy=(bar.get_x() + bar.get_width() / 2, height),
|
| 66 |
+
xytext=(0, 5),
|
| 67 |
+
textcoords="offset points",
|
| 68 |
+
ha='center', va='bottom', fontsize=12, fontweight='bold')
|
| 69 |
+
|
| 70 |
+
ax.set_xticks(x)
|
| 71 |
+
ax.set_xticklabels(pretty_labels, fontsize=13, fontweight='bold')
|
| 72 |
+
ax.set_ylim(0.5, 4.5)
|
| 73 |
+
ax.set_yticks([1, 2, 3, 4])
|
| 74 |
+
ax.set_yticklabels(['Strongly Disagree', 'Disagree', 'Agree', 'Strongly Agree'], fontsize=12)
|
| 75 |
+
ax.set_ylabel('Likert Score', fontsize=13, fontweight='bold')
|
| 76 |
+
ax.set_title('Identifier 7: Pre vs Post Likert Scores', fontsize=15, fontweight='bold')
|
| 77 |
+
ax.legend(fontsize=12)
|
| 78 |
+
ax.grid(axis='y', linestyle='--', alpha=0.7)
|
| 79 |
+
|
| 80 |
+
plt.tight_layout()
|
| 81 |
+
|
| 82 |
+
return fig
|
| 83 |
+
|
| 84 |
+
# Gradio interface
|
| 85 |
+
demo = gr.Interface(
|
| 86 |
+
fn=plot_likert_scores,
|
| 87 |
+
inputs=gr.File(label="Upload your Excel file (.xlsx)"),
|
| 88 |
+
outputs=gr.Plot(label="Likert Plot for Identifier 7"),
|
| 89 |
+
title="Pre vs Post Likert Plot (Identifier 7)",
|
| 90 |
+
description="Upload your survey Excel file. This tool compares pre/post Likert scores for Confidence, Feedback, and Enjoyment for respondent 7.",
|
| 91 |
+
allow_flagging='never'
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
demo.launch()
|