Spaces:
Running
Running
ram usage optimization
Browse files
app.py
CHANGED
|
@@ -7,6 +7,13 @@ from deep_translator import GoogleTranslator
|
|
| 7 |
from langdetect import detect, DetectorFactory
|
| 8 |
import time
|
| 9 |
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# --- CARICAMENTO ETICHETTE I18N ---
|
| 12 |
def load_labels(filepath="lang.yaml"):
|
|
@@ -56,9 +63,13 @@ except Exception as e:
|
|
| 56 |
print(f"Errore Qdrant: {e}")
|
| 57 |
client = None
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
COLLECTION_NAME = "books_collection"
|
| 64 |
VECTOR_SIZE = 256
|
|
@@ -105,12 +116,10 @@ LOADING_HTML = """
|
|
| 105 |
|
| 106 |
def render_results(ids, scores, ui_lang="it"):
|
| 107 |
if not ids: return L("error_no_results", ui_lang)
|
| 108 |
-
if not
|
| 109 |
|
| 110 |
-
conn = get_turso_conn()
|
| 111 |
placeholders = ", ".join(["?"] * len(ids))
|
| 112 |
-
rows =
|
| 113 |
-
conn.close()
|
| 114 |
|
| 115 |
books_map = {row[0]: row for row in rows}
|
| 116 |
ordered_books = [books_map[uid] for uid in ids if uid in books_map]
|
|
@@ -199,9 +208,11 @@ def search_logic(query, max_res, lang_pref):
|
|
| 199 |
def find_logic(title):
|
| 200 |
loading_html_dynamic = LOADING_HTML.replace("__LOADING_TEXT__", L("search_loading_message", "en"))
|
| 201 |
yield gr.update(loading_html_dynamic, visible=True), gr.update(visible=False), [], gr.update(visible=False)
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
| 205 |
|
| 206 |
# DATI PER LA TABELLA
|
| 207 |
data = [[str(r[1]), str(r[2]), str(r[3])] for r in rows]
|
|
|
|
| 7 |
from langdetect import detect, DetectorFactory
|
| 8 |
import time
|
| 9 |
import yaml
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
os.environ["OMP_NUM_THREADS"] = "1"
|
| 13 |
+
os.environ["MKL_NUM_THREADS"] = "1"
|
| 14 |
+
|
| 15 |
+
import torch
|
| 16 |
+
torch.set_num_threads(1)
|
| 17 |
|
| 18 |
# --- CARICAMENTO ETICHETTE I18N ---
|
| 19 |
def load_labels(filepath="lang.yaml"):
|
|
|
|
| 63 |
print(f"Errore Qdrant: {e}")
|
| 64 |
client = None
|
| 65 |
|
| 66 |
+
turso_conn = None
|
| 67 |
+
if TURSO_URL and TURSO_TOKEN:
|
| 68 |
+
try:
|
| 69 |
+
turso_conn = libsql.connect(TURSO_URL, auth_token=TURSO_TOKEN)
|
| 70 |
+
print("Connessione a Turso stabilita con successo.")
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"Errore connessione Turso: {e}")
|
| 73 |
|
| 74 |
COLLECTION_NAME = "books_collection"
|
| 75 |
VECTOR_SIZE = 256
|
|
|
|
| 116 |
|
| 117 |
def render_results(ids, scores, ui_lang="it"):
|
| 118 |
if not ids: return L("error_no_results", ui_lang)
|
| 119 |
+
if not turso_conn: return L("error_db_not_configured", ui_lang)
|
| 120 |
|
|
|
|
| 121 |
placeholders = ", ".join(["?"] * len(ids))
|
| 122 |
+
rows = turso_conn.execute(f"SELECT id, title, author, year, rating, summary FROM books WHERE id IN ({placeholders})", tuple(ids)).fetchall() conn.close()
|
|
|
|
| 123 |
|
| 124 |
books_map = {row[0]: row for row in rows}
|
| 125 |
ordered_books = [books_map[uid] for uid in ids if uid in books_map]
|
|
|
|
| 208 |
def find_logic(title):
|
| 209 |
loading_html_dynamic = LOADING_HTML.replace("__LOADING_TEXT__", L("search_loading_message", "en"))
|
| 210 |
yield gr.update(loading_html_dynamic, visible=True), gr.update(visible=False), [], gr.update(visible=False)
|
| 211 |
+
|
| 212 |
+
if not turso_conn:
|
| 213 |
+
yield gr.update(visible=False), gr.update(visible=True), [], gr.update(value="Database non connesso.", visible=True)
|
| 214 |
+
return
|
| 215 |
+
rows = turso_conn.execute(f"SELECT id, title, author, year FROM books WHERE title LIKE '%{title}%' LIMIT 10").fetchall() conn.close()
|
| 216 |
|
| 217 |
# DATI PER LA TABELLA
|
| 218 |
data = [[str(r[1]), str(r[2]), str(r[3])] for r in rows]
|