Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import re
|
4 |
+
from datetime import datetime
|
5 |
+
import pdfplumber
|
6 |
+
import plotly.graph_objects as go
|
7 |
+
import plotly.express as px
|
8 |
+
import io
|
9 |
+
|
10 |
+
def extract_data_from_pdf(pdf_content):
|
11 |
+
data_list = []
|
12 |
+
current_record = {}
|
13 |
+
|
14 |
+
for line in pdf_content.split('\n'):
|
15 |
+
# Extract header information
|
16 |
+
if 'Entreprise::' in line:
|
17 |
+
if current_record and 'temperature_data' in current_record:
|
18 |
+
data_list.append(current_record)
|
19 |
+
current_record = {'temperature_data': []}
|
20 |
+
|
21 |
+
# Extract metadata
|
22 |
+
if 'Date:' in line:
|
23 |
+
try:
|
24 |
+
date_str = re.search(r'Date:\s*(\d{2}[./]\d{2}[./]\d{4})', line).group(1)
|
25 |
+
date_str = date_str.replace('/', '.')
|
26 |
+
current_record['date'] = datetime.strptime(date_str, '%d.%m.%Y').strftime('%Y-%m-%d')
|
27 |
+
except:
|
28 |
+
current_record['date'] = None
|
29 |
+
|
30 |
+
if 'Produit:' in line:
|
31 |
+
current_record['produit'] = line.split('Produit:')[-1].strip()
|
32 |
+
|
33 |
+
if 'Utilisateur:' in line:
|
34 |
+
current_record['utilisateur'] = line.split('Utilisateur:')[-1].strip()
|
35 |
+
|
36 |
+
# Extract temperature data
|
37 |
+
if any(x in line for x in ['Début', '+ ', 'Fin']) and '°C' in line:
|
38 |
+
try:
|
39 |
+
parts = line.strip().split()
|
40 |
+
time = parts[0] if 'Début' in line or 'Fin' in line else parts[1]
|
41 |
+
temp_sterilisateur = float(parts[-3].replace('°C', ''))
|
42 |
+
temp_coeur = float(parts[-2].replace('°C', ''))
|
43 |
+
valeur_f = float(parts[-1])
|
44 |
+
|
45 |
+
current_record['temperature_data'].append({
|
46 |
+
'temps': time,
|
47 |
+
'temp_sterilisateur': temp_sterilisateur,
|
48 |
+
'temp_coeur': temp_coeur,
|
49 |
+
'valeur_f': valeur_f
|
50 |
+
})
|
51 |
+
except:
|
52 |
+
continue
|
53 |
+
|
54 |
+
# Add last record
|
55 |
+
if current_record and 'temperature_data' in current_record:
|
56 |
+
data_list.append(current_record)
|
57 |
+
|
58 |
+
return data_list
|
59 |
+
|
60 |
+
def analyze_sterilization(data):
|
61 |
+
results = []
|
62 |
+
|
63 |
+
for record in data:
|
64 |
+
temp_data = pd.DataFrame(record['temperature_data'])
|
65 |
+
|
66 |
+
# Determine product type and required temperature
|
67 |
+
is_nutabreizh = 'NutaBreizh' in record['produit']
|
68 |
+
required_temp = 108 if is_nutabreizh else 103
|
69 |
+
|
70 |
+
# Count minutes at required temperature
|
71 |
+
minutes_at_temp = len(temp_data[temp_data['temp_coeur'] >= required_temp])
|
72 |
+
|
73 |
+
# Calculate max temperatures
|
74 |
+
max_temp_sterilisateur = temp_data['temp_sterilisateur'].max()
|
75 |
+
max_temp_coeur = temp_data['temp_coeur'].max()
|
76 |
+
|
77 |
+
# Determine if criteria met
|
78 |
+
criteria_met = minutes_at_temp >= 30
|
79 |
+
|
80 |
+
results.append({
|
81 |
+
'Date': record['date'],
|
82 |
+
'Produit': record['produit'],
|
83 |
+
'Utilisateur': record['utilisateur'],
|
84 |
+
'Temperature_Requise': required_temp,
|
85 |
+
'Minutes_Temperature_Requise': minutes_at_temp,
|
86 |
+
'Temperature_Max_Sterilisateur': max_temp_sterilisateur,
|
87 |
+
'Temperature_Max_Coeur': max_temp_coeur,
|
88 |
+
'Criteres_Respectes': criteria_met
|
89 |
+
})
|
90 |
+
|
91 |
+
return pd.DataFrame(results)
|
92 |
+
|
93 |
+
def main():
|
94 |
+
st.title("Analyse des Protocoles de Stérilisation")
|
95 |
+
|
96 |
+
uploaded_file = st.file_uploader("Choisir un fichier PDF", type="pdf")
|
97 |
+
|
98 |
+
if uploaded_file is not None:
|
99 |
+
# Read PDF content
|
100 |
+
pdf_text = ""
|
101 |
+
with pdfplumber.open(uploaded_file) as pdf:
|
102 |
+
for page in pdf.pages:
|
103 |
+
pdf_text += page.extract_text() + "\n"
|
104 |
+
|
105 |
+
# Process data
|
106 |
+
data = extract_data_from_pdf(pdf_text)
|
107 |
+
results_df = analyze_sterilization(data)
|
108 |
+
|
109 |
+
# Display results
|
110 |
+
st.subheader("Résultats de l'analyse")
|
111 |
+
st.dataframe(results_df)
|
112 |
+
|
113 |
+
# Create visualization
|
114 |
+
fig = px.scatter(results_df,
|
115 |
+
x='Date',
|
116 |
+
y='Minutes_Temperature_Requise',
|
117 |
+
color='Criteres_Respectes',
|
118 |
+
hover_data=['Produit', 'Temperature_Requise'],
|
119 |
+
title="Minutes à température requise par production")
|
120 |
+
st.plotly_chart(fig)
|
121 |
+
|
122 |
+
# Export button
|
123 |
+
if st.button("Exporter en Excel"):
|
124 |
+
output = io.BytesIO()
|
125 |
+
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
|
126 |
+
results_df.to_excel(writer, index=False)
|
127 |
+
|
128 |
+
output.seek(0)
|
129 |
+
st.download_button(
|
130 |
+
label="Télécharger l'analyse",
|
131 |
+
data=output,
|
132 |
+
file_name="analyse_sterilisation.xlsx",
|
133 |
+
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
134 |
+
)
|
135 |
+
|
136 |
+
if __name__ == "__main__":
|
137 |
+
main()
|