Spaces:
Sleeping
Sleeping
Vaishnav14220
commited on
Commit
Β·
61713c9
1
Parent(s):
4b67935
Add database update functionality with admin password protection and logging
Browse files
app.py
CHANGED
|
@@ -20,8 +20,8 @@ from svglib.svglib import svg2rlg
|
|
| 20 |
dataset = load_dataset("smitathkr1/organic_reactions_corrected")
|
| 21 |
df = dataset['train'].to_pandas()
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
all_reactants = []
|
| 26 |
all_products = []
|
| 27 |
|
|
@@ -269,23 +269,53 @@ def fix_reaction_with_gemini(reaction_name, api_key):
|
|
| 269 |
import json
|
| 270 |
gemini_data = json.loads(response_text)
|
| 271 |
|
| 272 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
global df
|
| 274 |
-
mask = df['corrected_name'].str.lower() == reaction_name.lower()
|
| 275 |
if mask.any():
|
| 276 |
idx = df[mask].index[0]
|
| 277 |
-
df.at[idx, 'corrected_name'] =
|
| 278 |
-
df.at[idx, 'general_reactants'] = ', '.join(
|
| 279 |
-
df.at[idx, 'general_reagents'] = ', '.join(
|
| 280 |
-
df.at[idx, 'general_products'] = ', '.join(
|
| 281 |
-
df.at[idx, 'description'] =
|
| 282 |
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
else:
|
| 285 |
-
return f"Reaction '{reaction_name}' not found in database."
|
| 286 |
|
| 287 |
except Exception as e:
|
| 288 |
-
return f"Error
|
| 289 |
|
| 290 |
def get_autocomplete_reactants(query):
|
| 291 |
if not query:
|
|
@@ -346,6 +376,16 @@ with gr.Blocks(title="Organic Reactions Search") as demo:
|
|
| 346 |
|
| 347 |
fix_button.click(fix_reaction_with_gemini, inputs=[reaction_to_fix, api_key_input], outputs=ai_status)
|
| 348 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
gr.Markdown("---")
|
| 350 |
gr.Markdown("**Database Table:**")
|
| 351 |
|
|
|
|
| 20 |
dataset = load_dataset("smitathkr1/organic_reactions_corrected")
|
| 21 |
df = dataset['train'].to_pandas()
|
| 22 |
|
| 23 |
+
# Global variable to store the last AI fix for potential database update
|
| 24 |
+
last_ai_fix = None
|
| 25 |
all_reactants = []
|
| 26 |
all_products = []
|
| 27 |
|
|
|
|
| 269 |
import json
|
| 270 |
gemini_data = json.loads(response_text)
|
| 271 |
|
| 272 |
+
# Store the updated data globally for potential database update
|
| 273 |
+
global last_ai_fix
|
| 274 |
+
last_ai_fix = {
|
| 275 |
+
'reaction_name': reaction_name,
|
| 276 |
+
'updated_data': gemini_data,
|
| 277 |
+
'timestamp': str(pd.Timestamp.now())
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
return f"β
**AI Fix Completed for '{reaction_name}'**\n\n**Updated Data:**\n- **Name:** {gemini_data.get('reaction name', 'N/A')}\n- **Reactants:** {', '.join(gemini_data.get('reactants', []))}\n- **Reagents:** {', '.join(gemini_data.get('reagents', []))}\n- **Products:** {', '.join(gemini_data.get('products', []))}\n- **Description:** {gemini_data.get('description', '')[:200]}...\n\nπ‘ **To save this fix to the database, enter the admin password below and click 'Update Database'.**"
|
| 281 |
+
|
| 282 |
+
except Exception as e:
|
| 283 |
+
return f"β Error calling Gemini API: {str(e)}"
|
| 284 |
+
|
| 285 |
+
def update_database_with_ai_fix(password):
|
| 286 |
+
if password != "Vvn@#411037":
|
| 287 |
+
return "β Incorrect password. Database update denied."
|
| 288 |
+
|
| 289 |
+
global last_ai_fix
|
| 290 |
+
if not last_ai_fix:
|
| 291 |
+
return "β No recent AI fix to save. Please fix a reaction first."
|
| 292 |
+
|
| 293 |
+
try:
|
| 294 |
+
# Update the global df
|
| 295 |
global df
|
| 296 |
+
mask = df['corrected_name'].str.lower() == last_ai_fix['reaction_name'].lower()
|
| 297 |
if mask.any():
|
| 298 |
idx = df[mask].index[0]
|
| 299 |
+
df.at[idx, 'corrected_name'] = last_ai_fix['updated_data'].get('reaction name', last_ai_fix['reaction_name'])
|
| 300 |
+
df.at[idx, 'general_reactants'] = ', '.join(last_ai_fix['updated_data'].get('reactants', []))
|
| 301 |
+
df.at[idx, 'general_reagents'] = ', '.join(last_ai_fix['updated_data'].get('reagents', []))
|
| 302 |
+
df.at[idx, 'general_products'] = ', '.join(last_ai_fix['updated_data'].get('products', []))
|
| 303 |
+
df.at[idx, 'description'] = last_ai_fix['updated_data'].get('description', df.at[idx, 'description'])
|
| 304 |
|
| 305 |
+
# Log the update
|
| 306 |
+
log_entry = f"[{last_ai_fix['timestamp']}] Database updated: '{last_ai_fix['reaction_name']}' -> '{last_ai_fix['updated_data'].get('reaction name', last_ai_fix['reaction_name'])}'\n"
|
| 307 |
+
with open('database_updates.log', 'a') as f:
|
| 308 |
+
f.write(log_entry)
|
| 309 |
+
|
| 310 |
+
# Clear the last fix
|
| 311 |
+
last_ai_fix = None
|
| 312 |
+
|
| 313 |
+
return f"β
**Database Updated Successfully!**\n\nThe reaction has been permanently updated in the current session. Changes logged to 'database_updates.log'.\n\n**Note:** Since this is a Hugging Face Space, the changes are session-only. To make permanent changes, please update your local dataset and re-upload."
|
| 314 |
else:
|
| 315 |
+
return f"β Reaction '{last_ai_fix['reaction_name']}' not found in database."
|
| 316 |
|
| 317 |
except Exception as e:
|
| 318 |
+
return f"β Error updating database: {str(e)}"
|
| 319 |
|
| 320 |
def get_autocomplete_reactants(query):
|
| 321 |
if not query:
|
|
|
|
| 376 |
|
| 377 |
fix_button.click(fix_reaction_with_gemini, inputs=[reaction_to_fix, api_key_input], outputs=ai_status)
|
| 378 |
|
| 379 |
+
# Database update section
|
| 380 |
+
gr.Markdown("---")
|
| 381 |
+
gr.Markdown("**Database Update (Admin Only):**")
|
| 382 |
+
with gr.Row():
|
| 383 |
+
admin_password = gr.Textbox(label="Admin Password", type="password", placeholder="Enter admin password to update database")
|
| 384 |
+
update_db_button = gr.Button("Update Database", variant="secondary")
|
| 385 |
+
|
| 386 |
+
update_status = gr.Markdown(label="Update Status")
|
| 387 |
+
update_db_button.click(update_database_with_ai_fix, inputs=[admin_password], outputs=update_status)
|
| 388 |
+
|
| 389 |
gr.Markdown("---")
|
| 390 |
gr.Markdown("**Database Table:**")
|
| 391 |
|