Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import logging
|
2 |
-
logging.basicConfig(level=logging.INFO)
|
3 |
import datetime
|
4 |
import io
|
5 |
import base64
|
@@ -15,6 +14,9 @@ from azure.cosmos.exceptions import CosmosHttpResponseError
|
|
15 |
from pymongo import MongoClient
|
16 |
from pymongo.server_api import ServerApi
|
17 |
|
|
|
|
|
|
|
18 |
from dotenv import load_dotenv
|
19 |
load_dotenv()
|
20 |
|
@@ -51,30 +53,28 @@ if not cosmos_mongodb_connection_string:
|
|
51 |
|
52 |
try:
|
53 |
#Crear el cliente de MongoDB
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
tlsAllowInvalidCertificates=True
|
62 |
-
)
|
63 |
|
64 |
#Forzar una conexi贸n para verificar
|
65 |
mongo_client.admin.command('ping')
|
66 |
|
67 |
#Seleccionar la base de datos
|
68 |
-
|
69 |
|
70 |
-
#Seleccionar
|
71 |
-
|
|
|
72 |
|
73 |
# Prueba de conexi贸n
|
74 |
mongo_client.server_info()
|
75 |
print("Conexi贸n a MongoDB API exitosa")
|
76 |
|
77 |
-
#Verificar que la colecci贸n existe
|
78 |
# Verificar que la colecci贸n existe
|
79 |
if 'text_analysis' in mongo_db.list_collection_names():
|
80 |
print("Colecci贸n 'text_analysis' encontrada")
|
@@ -82,9 +82,19 @@ try:
|
|
82 |
print("Advertencia: La colecci贸n 'text_analysis' no existe. Se crear谩 autom谩ticamente al insertar el primer documento.")
|
83 |
|
84 |
except Exception as e:
|
85 |
-
|
86 |
raise
|
87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
# Configure the page to use the full width
|
89 |
st.set_page_config(
|
90 |
page_title="AIdeaText",
|
|
|
1 |
import logging
|
|
|
2 |
import datetime
|
3 |
import io
|
4 |
import base64
|
|
|
14 |
from pymongo import MongoClient
|
15 |
from pymongo.server_api import ServerApi
|
16 |
|
17 |
+
logging.basicConfig(level=logging.DEBUG)
|
18 |
+
logger = logging.getLogger(__name__)
|
19 |
+
|
20 |
from dotenv import load_dotenv
|
21 |
load_dotenv()
|
22 |
|
|
|
53 |
|
54 |
try:
|
55 |
#Crear el cliente de MongoDB
|
56 |
+
client = MongoClient(cosmos_mongodb_connection_string,
|
57 |
+
tls=True,
|
58 |
+
tlsCAFile=certifi.where(),
|
59 |
+
retryWrites=False,
|
60 |
+
serverSelectionTimeoutMS=5000,
|
61 |
+
connectTimeoutMS=10000,
|
62 |
+
socketTimeoutMS=10000)
|
|
|
|
|
63 |
|
64 |
#Forzar una conexi贸n para verificar
|
65 |
mongo_client.admin.command('ping')
|
66 |
|
67 |
#Seleccionar la base de datos
|
68 |
+
client.admin.command('ismaster')
|
69 |
|
70 |
+
#Seleccionar la base de datos
|
71 |
+
db = client['aideatext_db']
|
72 |
+
analysis_collection = db['text_analysis']
|
73 |
|
74 |
# Prueba de conexi贸n
|
75 |
mongo_client.server_info()
|
76 |
print("Conexi贸n a MongoDB API exitosa")
|
77 |
|
|
|
78 |
# Verificar que la colecci贸n existe
|
79 |
if 'text_analysis' in mongo_db.list_collection_names():
|
80 |
print("Colecci贸n 'text_analysis' encontrada")
|
|
|
82 |
print("Advertencia: La colecci贸n 'text_analysis' no existe. Se crear谩 autom谩ticamente al insertar el primer documento.")
|
83 |
|
84 |
except Exception as e:
|
85 |
+
logger.error(f"Error al conectar con Cosmos DB MongoDB API: {str(e)}", exc_info=True)
|
86 |
raise
|
87 |
|
88 |
+
# Funci贸n para insertar un documento
|
89 |
+
def insert_document(document):
|
90 |
+
try:
|
91 |
+
result = analysis_collection.insert_one(document)
|
92 |
+
logger.info(f"Documento insertado con ID: {result.inserted_id}")
|
93 |
+
return result.inserted_id
|
94 |
+
except Exception as e:
|
95 |
+
logger.error(f"Error al insertar documento: {str(e)}", exc_info=True)
|
96 |
+
return None
|
97 |
+
|
98 |
# Configure the page to use the full width
|
99 |
st.set_page_config(
|
100 |
page_title="AIdeaText",
|