Vaishnav14220 commited on
Commit
e836a09
Β·
1 Parent(s): 2b57822

Fix KeyError by removing references to non-existent columns reactants/products/reagents

Browse files
Files changed (1) hide show
  1. app.py +21 -19
app.py CHANGED
@@ -23,8 +23,10 @@ all_reactants = []
23
  all_products = []
24
 
25
  for _, row in df.iterrows():
26
- all_reactants.extend(row['reactants'])
27
- all_products.extend(row['products'])
 
 
28
 
29
  unique_reactants = list(set(all_reactants))
30
  unique_products = list(set(all_products))
@@ -103,9 +105,9 @@ def generate_all_reactions_pdf():
103
  products_smiles = [s for s in row['products_smiles'] if s is not None]
104
 
105
  # Content
106
- reactants = row['general_reactants'] if pd.notna(row['general_reactants']) else ', '.join(row['reactants'])
107
- reagents = row['general_reagents'] if pd.notna(row['general_reagents']) else ', '.join(row['reagents'])
108
- products = row['general_products'] if pd.notna(row['general_products']) else ', '.join(row['products'])
109
 
110
  content = [
111
  f"<b>Reactants:</b> {reactants}",
@@ -131,9 +133,9 @@ def search_by_reaction_name(query):
131
  result = df[df['corrected_name'].str.lower() == query.lower()]
132
  if not result.empty:
133
  row = result.iloc[0]
134
- reactants = row['general_reactants'] if pd.notna(row['general_reactants']) else ', '.join(row['reactants'])
135
- products = row['general_products'] if pd.notna(row['general_products']) else ', '.join(row['products'])
136
- reagents = row['general_reagents'] if pd.notna(row['general_reagents']) else ', '.join(row['reagents'])
137
  return f"**{row['corrected_name']}**\n\n**Reactants:** {reactants}\n\n**Reagents:** {reagents}\n\n**Products:** {products}\n\n**Description:** {row['description'][:500]}..."
138
  # Fuzzy match
139
  matches = process.extract(query, reaction_names, limit=1)
@@ -141,9 +143,9 @@ def search_by_reaction_name(query):
141
  best_match = matches[0][0]
142
  result = df[df['corrected_name'] == best_match]
143
  row = result.iloc[0]
144
- reactants = row['general_reactants'] if pd.notna(row['general_reactants']) else ', '.join(row['reactants'])
145
- products = row['general_products'] if pd.notna(row['general_products']) else ', '.join(row['products'])
146
- reagents = row['general_reagents'] if pd.notna(row['general_reagents']) else ', '.join(row['reagents'])
147
  return f"**{row['corrected_name']}** (closest match)\n\n**Reactants:** {reactants}\n\n**Reagents:** {reagents}\n\n**Products:** {products}\n\n**Description:** {row['description'][:500]}..."
148
  return "No matching reaction found."
149
 
@@ -154,8 +156,8 @@ def search_by_reactant(reactant):
154
  if not matches.empty:
155
  results = []
156
  for _, row in matches.head(5).iterrows():
157
- reactants = row['general_reactants'] if pd.notna(row['general_reactants']) else ', '.join(row['reactants'])
158
- products = row['general_products'] if pd.notna(row['general_products']) else ', '.join(row['products'])
159
  results.append(f"**{row['corrected_name']}**: {reactants} β†’ {products}")
160
  return "\n\n".join(results)
161
  # Fuzzy match for autocorrection
@@ -166,8 +168,8 @@ def search_by_reactant(reactant):
166
  if not matches.empty:
167
  results = [f"Did you mean '{closest}'?\n"]
168
  for _, row in matches.head(5).iterrows():
169
- reactants = row['general_reactants'] if pd.notna(row['general_reactants']) else ', '.join(row['reactants'])
170
- products = row['general_products'] if pd.notna(row['general_products']) else ', '.join(row['products'])
171
  results.append(f"**{row['corrected_name']}**: {reactants} β†’ {products}")
172
  return "\n\n".join(results)
173
  return "No reactions found with that reactant."
@@ -179,8 +181,8 @@ def search_by_product(product):
179
  if not matches.empty:
180
  results = []
181
  for _, row in matches.head(5).iterrows():
182
- reactants = row['general_reactants'] if pd.notna(row['general_reactants']) else ', '.join(row['reactants'])
183
- products = row['general_products'] if pd.notna(row['general_products']) else ', '.join(row['products'])
184
  results.append(f"**{row['corrected_name']}**: {reactants} β†’ {products}")
185
  return "\n\n".join(results)
186
  # Fuzzy match for autocorrection
@@ -191,8 +193,8 @@ def search_by_product(product):
191
  if not matches.empty:
192
  results = [f"Did you mean '{closest}'?\n"]
193
  for _, row in matches.head(5).iterrows():
194
- reactants = row['general_reactants'] if pd.notna(row['general_reactants']) else ', '.join(row['reactants'])
195
- products = row['general_products'] if pd.notna(row['general_products']) else ', '.join(row['products'])
196
  results.append(f"**{row['corrected_name']}**: {reactants} β†’ {products}")
197
  return "\n\n".join(results)
198
  return "No reactions found with that product."
 
23
  all_products = []
24
 
25
  for _, row in df.iterrows():
26
+ if pd.notna(row['general_reactants']):
27
+ all_reactants.append(row['general_reactants'])
28
+ if pd.notna(row['general_products']):
29
+ all_products.append(row['general_products'])
30
 
31
  unique_reactants = list(set(all_reactants))
32
  unique_products = list(set(all_products))
 
105
  products_smiles = [s for s in row['products_smiles'] if s is not None]
106
 
107
  # Content
108
+ reactants = row['general_reactants'] if pd.notna(row['general_reactants']) else 'N/A'
109
+ reagents = row['general_reagents'] if pd.notna(row['general_reagents']) else 'N/A'
110
+ products = row['general_products'] if pd.notna(row['general_products']) else 'N/A'
111
 
112
  content = [
113
  f"<b>Reactants:</b> {reactants}",
 
133
  result = df[df['corrected_name'].str.lower() == query.lower()]
134
  if not result.empty:
135
  row = result.iloc[0]
136
+ reactants = row['general_reactants'] if pd.notna(row['general_reactants']) else 'N/A'
137
+ products = row['general_products'] if pd.notna(row['general_products']) else 'N/A'
138
+ reagents = row['general_reagents'] if pd.notna(row['general_reagents']) else 'N/A'
139
  return f"**{row['corrected_name']}**\n\n**Reactants:** {reactants}\n\n**Reagents:** {reagents}\n\n**Products:** {products}\n\n**Description:** {row['description'][:500]}..."
140
  # Fuzzy match
141
  matches = process.extract(query, reaction_names, limit=1)
 
143
  best_match = matches[0][0]
144
  result = df[df['corrected_name'] == best_match]
145
  row = result.iloc[0]
146
+ reactants = row['general_reactants'] if pd.notna(row['general_reactants']) else 'N/A'
147
+ products = row['general_products'] if pd.notna(row['general_products']) else 'N/A'
148
+ reagents = row['general_reagents'] if pd.notna(row['general_reagents']) else 'N/A'
149
  return f"**{row['corrected_name']}** (closest match)\n\n**Reactants:** {reactants}\n\n**Reagents:** {reagents}\n\n**Products:** {products}\n\n**Description:** {row['description'][:500]}..."
150
  return "No matching reaction found."
151
 
 
156
  if not matches.empty:
157
  results = []
158
  for _, row in matches.head(5).iterrows():
159
+ reactants = row['general_reactants'] if pd.notna(row['general_reactants']) else 'N/A'
160
+ products = row['general_products'] if pd.notna(row['general_products']) else 'N/A'
161
  results.append(f"**{row['corrected_name']}**: {reactants} β†’ {products}")
162
  return "\n\n".join(results)
163
  # Fuzzy match for autocorrection
 
168
  if not matches.empty:
169
  results = [f"Did you mean '{closest}'?\n"]
170
  for _, row in matches.head(5).iterrows():
171
+ reactants = row['general_reactants'] if pd.notna(row['general_reactants']) else 'N/A'
172
+ products = row['general_products'] if pd.notna(row['general_products']) else 'N/A'
173
  results.append(f"**{row['corrected_name']}**: {reactants} β†’ {products}")
174
  return "\n\n".join(results)
175
  return "No reactions found with that reactant."
 
181
  if not matches.empty:
182
  results = []
183
  for _, row in matches.head(5).iterrows():
184
+ reactants = row['general_reactants'] if pd.notna(row['general_reactants']) else 'N/A'
185
+ products = row['general_products'] if pd.notna(row['general_products']) else 'N/A'
186
  results.append(f"**{row['corrected_name']}**: {reactants} β†’ {products}")
187
  return "\n\n".join(results)
188
  # Fuzzy match for autocorrection
 
193
  if not matches.empty:
194
  results = [f"Did you mean '{closest}'?\n"]
195
  for _, row in matches.head(5).iterrows():
196
+ reactants = row['general_reactants'] if pd.notna(row['general_reactants']) else 'N/A'
197
+ products = row['general_products'] if pd.notna(row['general_products']) else 'N/A'
198
  results.append(f"**{row['corrected_name']}**: {reactants} β†’ {products}")
199
  return "\n\n".join(results)
200
  return "No reactions found with that product."