Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ import streamlit as st
|
|
| 10 |
import os
|
| 11 |
from groq import Groq
|
| 12 |
import time
|
|
|
|
| 13 |
|
| 14 |
def clean_text(text):
|
| 15 |
return re.sub(r'\s+', ' ', text).strip()
|
|
@@ -154,12 +155,15 @@ def get_elenco_comuni(provincia):
|
|
| 154 |
cerca_premuto = False
|
| 155 |
comuni_provincia = {}
|
| 156 |
|
| 157 |
-
def scrivi_dataframe(output, riepilogo, comune):
|
| 158 |
-
if len(output) > 0:
|
| 159 |
st.numero_immobili_validi = st.numero_immobili_validi + 1
|
| 160 |
if not riepilogo:
|
| 161 |
st.write(f"### {comune}")
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
| 163 |
df_originale = df.sort_values(by=["Vantaggio", "PrezzoMq"], ascending=[False, True])
|
| 164 |
if not riepilogo:
|
| 165 |
columns_to_display = ["Vantaggioso", "Vantaggio", "Immagine", "Titolo", "PrezzoMq", "Prezzo", "Superficie", "Locali", "PrezzoMedioMq", "Link"]
|
|
@@ -386,21 +390,58 @@ with st.sidebar:
|
|
| 386 |
|
| 387 |
cerca_premuto = st.button("Cerca", use_container_width=True, type='primary')
|
| 388 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
if cerca_premuto:
|
| 390 |
dfs = []
|
| 391 |
if uploaded_files:
|
| 392 |
for file in uploaded_files:
|
| 393 |
-
|
| 394 |
-
df = pd.read_csv(file)
|
| 395 |
-
elif file.name.endswith('.xlsx') or file.name.endswith('.xls'):
|
| 396 |
-
df = pd.read_excel(file)
|
| 397 |
-
else:
|
| 398 |
-
st.error(f"Formato di file non supportato: {file.name}")
|
| 399 |
-
continue
|
| 400 |
dfs.append(df)
|
| 401 |
if dfs:
|
| 402 |
concatenated_df = pd.concat(dfs, ignore_index=True)
|
| 403 |
-
|
|
|
|
|
|
|
| 404 |
|
| 405 |
if len(comune_input)>0:
|
| 406 |
comuni_selezionati = comune_input
|
|
@@ -448,8 +489,9 @@ if cerca_premuto:
|
|
| 448 |
#st.write("### Dettagliata")
|
| 449 |
#analizza_dati_ai(output, 2)
|
| 450 |
st.success("Elaborazione Completata")
|
| 451 |
-
|
| 452 |
-
|
|
|
|
| 453 |
|
| 454 |
#if 'initialized' not in st.session_state:
|
| 455 |
# st.session_state['initialized'] = True
|
|
|
|
| 10 |
import os
|
| 11 |
from groq import Groq
|
| 12 |
import time
|
| 13 |
+
from openpyxl import load_workbook
|
| 14 |
|
| 15 |
def clean_text(text):
|
| 16 |
return re.sub(r'\s+', ' ', text).strip()
|
|
|
|
| 155 |
cerca_premuto = False
|
| 156 |
comuni_provincia = {}
|
| 157 |
|
| 158 |
+
def scrivi_dataframe(output, riepilogo, comune, da_excel = False, df_excel = None):
|
| 159 |
+
if len(output) > 0 or (da_excel and df_excel is not None):
|
| 160 |
st.numero_immobili_validi = st.numero_immobili_validi + 1
|
| 161 |
if not riepilogo:
|
| 162 |
st.write(f"### {comune}")
|
| 163 |
+
if da_excel:
|
| 164 |
+
df = df_excel
|
| 165 |
+
else:
|
| 166 |
+
df = pd.DataFrame(output)
|
| 167 |
df_originale = df.sort_values(by=["Vantaggio", "PrezzoMq"], ascending=[False, True])
|
| 168 |
if not riepilogo:
|
| 169 |
columns_to_display = ["Vantaggioso", "Vantaggio", "Immagine", "Titolo", "PrezzoMq", "Prezzo", "Superficie", "Locali", "PrezzoMedioMq", "Link"]
|
|
|
|
| 390 |
|
| 391 |
cerca_premuto = st.button("Cerca", use_container_width=True, type='primary')
|
| 392 |
|
| 393 |
+
def importa_excel(file):
|
| 394 |
+
df = None
|
| 395 |
+
if file.name.endswith('.csv'):
|
| 396 |
+
df = pd.read_csv(file)
|
| 397 |
+
elif file.name.endswith('.xlsx') or file.name.endswith('.xls'):
|
| 398 |
+
df = pd.read_excel(file)
|
| 399 |
+
else:
|
| 400 |
+
st.error(f"Formato di file non supportato: {file.name}")
|
| 401 |
+
if df is not None:
|
| 402 |
+
wb = load_workbook(file)
|
| 403 |
+
ws = wb.active
|
| 404 |
+
if df.columns[0] != "Comune":
|
| 405 |
+
df = pd.read_excel(file, header=1)
|
| 406 |
+
if 'Link asta' in df.columns:
|
| 407 |
+
link_column_index = df.columns.get_loc("Link asta") + 1
|
| 408 |
+
for i in range(2, ws.max_row + 1):
|
| 409 |
+
cell = ws.cell(row=i, column=link_column_index)
|
| 410 |
+
if cell.hyperlink:
|
| 411 |
+
df.at[i - 2, "Link asta"] = cell.hyperlink.target
|
| 412 |
+
if 'Perizia' in df.columns:
|
| 413 |
+
link_column_index = df.columns.get_loc("Perizia") + 1
|
| 414 |
+
for i in range(2, ws.max_row + 1):
|
| 415 |
+
cell = ws.cell(row=i, column=link_column_index)
|
| 416 |
+
if cell.hyperlink:
|
| 417 |
+
df.at[i - 2, "Perizia"] = cell.hyperlink.target
|
| 418 |
+
columns_to_display = ["Comune", "Off. min. [€]", "Locali [nr]", "Mq", "Descrizione", "Link asta", "Perizia"]
|
| 419 |
+
df = df[columns_to_display]
|
| 420 |
+
df = df.rename(columns={
|
| 421 |
+
"Off. min. [€]": 'Prezzo',
|
| 422 |
+
'Locali [nr]': 'Locali',
|
| 423 |
+
'Descrizione':'Titolo',
|
| 424 |
+
'Mq':'Superficie',
|
| 425 |
+
'Link asta': 'Link'
|
| 426 |
+
})
|
| 427 |
+
df['PrezzoMq'] = 0 #df['Prezzo'] / df['Superficie']
|
| 428 |
+
df['Vantaggioso'] = False
|
| 429 |
+
df['Vantaggio'] = 50
|
| 430 |
+
df['Immagine'] = ""
|
| 431 |
+
df['PrezzoMedioMq'] = 0
|
| 432 |
+
return df
|
| 433 |
+
|
| 434 |
if cerca_premuto:
|
| 435 |
dfs = []
|
| 436 |
if uploaded_files:
|
| 437 |
for file in uploaded_files:
|
| 438 |
+
df = importa_excel(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
dfs.append(df)
|
| 440 |
if dfs:
|
| 441 |
concatenated_df = pd.concat(dfs, ignore_index=True)
|
| 442 |
+
scrivi_dataframe([], False, 'Excel', True, concatenated_df)
|
| 443 |
+
#st.write(concatenated_df)
|
| 444 |
+
st.success("File Excel importati con successo")
|
| 445 |
|
| 446 |
if len(comune_input)>0:
|
| 447 |
comuni_selezionati = comune_input
|
|
|
|
| 489 |
#st.write("### Dettagliata")
|
| 490 |
#analizza_dati_ai(output, 2)
|
| 491 |
st.success("Elaborazione Completata")
|
| 492 |
+
|
| 493 |
+
if not uploaded_files and len(comune_input) == 0:
|
| 494 |
+
st.error("Per favore, inserisci il nome di un comune o seleziona un file excel")
|
| 495 |
|
| 496 |
#if 'initialized' not in st.session_state:
|
| 497 |
# st.session_state['initialized'] = True
|