Spaces:
Sleeping
Sleeping
Vaishnav14220
commited on
Commit
Β·
7a2f5d4
1
Parent(s):
17c3fa2
Add dropdown autocomplete and autocorrection for searches
Browse files
app.py
CHANGED
|
@@ -69,6 +69,16 @@ def search_by_reactant(reactant):
|
|
| 69 |
for _, row in matches.head(5).iterrows():
|
| 70 |
results.append(f"**{row['name']}**: {', '.join(row['reactants'])} β {', '.join(row['products'])}")
|
| 71 |
return "\n\n".join(results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
return "No reactions found with that reactant."
|
| 73 |
|
| 74 |
def search_by_product(product):
|
|
@@ -80,6 +90,16 @@ def search_by_product(product):
|
|
| 80 |
for _, row in matches.head(5).iterrows():
|
| 81 |
results.append(f"**{row['name']}**: {', '.join(row['reactants'])} β {', '.join(row['products'])}")
|
| 82 |
return "\n\n".join(results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
return "No reactions found with that product."
|
| 84 |
|
| 85 |
def get_autocomplete_reactions(query):
|
|
@@ -105,23 +125,29 @@ with gr.Blocks(title="Organic Reactions Search") as demo:
|
|
| 105 |
gr.Markdown("Search through the organic reactions dataset by name, reactant, or product.")
|
| 106 |
|
| 107 |
with gr.Tab("Search by Reaction Name"):
|
| 108 |
-
reaction_input = gr.
|
| 109 |
reaction_output = gr.Markdown(label="Result")
|
| 110 |
reaction_btn = gr.Button("Search")
|
| 111 |
reaction_btn.click(search_by_reaction_name, inputs=reaction_input, outputs=reaction_output)
|
| 112 |
|
| 113 |
with gr.Tab("Search by Reactant"):
|
| 114 |
-
reactant_input = gr.
|
| 115 |
reactant_output = gr.Markdown(label="Results")
|
| 116 |
reactant_btn = gr.Button("Search")
|
| 117 |
reactant_btn.click(search_by_reactant, inputs=reactant_input, outputs=reactant_output)
|
| 118 |
|
| 119 |
with gr.Tab("View Reaction SVG"):
|
| 120 |
-
svg_input = gr.
|
| 121 |
svg_output = gr.HTML(label="Reaction SVG")
|
| 122 |
svg_btn = gr.Button("Generate SVG")
|
| 123 |
svg_btn.click(generate_reaction_svg, inputs=svg_input, outputs=svg_output)
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
with gr.Tab("API Documentation"):
|
| 126 |
gr.Markdown("""
|
| 127 |
## API Endpoints
|
|
|
|
| 69 |
for _, row in matches.head(5).iterrows():
|
| 70 |
results.append(f"**{row['name']}**: {', '.join(row['reactants'])} β {', '.join(row['products'])}")
|
| 71 |
return "\n\n".join(results)
|
| 72 |
+
# Fuzzy match for autocorrection
|
| 73 |
+
fuzzy_matches = process.extract(reactant, unique_reactants, limit=3)
|
| 74 |
+
if fuzzy_matches and fuzzy_matches[0][1] > 70:
|
| 75 |
+
closest = fuzzy_matches[0][0]
|
| 76 |
+
matches = df[df['reactants'].apply(lambda x: closest.lower() in [r.lower() for r in x])]
|
| 77 |
+
if not matches.empty:
|
| 78 |
+
results = [f"Did you mean '{closest}'?\n"]
|
| 79 |
+
for _, row in matches.head(5).iterrows():
|
| 80 |
+
results.append(f"**{row['name']}**: {', '.join(row['reactants'])} β {', '.join(row['products'])}")
|
| 81 |
+
return "\n\n".join(results)
|
| 82 |
return "No reactions found with that reactant."
|
| 83 |
|
| 84 |
def search_by_product(product):
|
|
|
|
| 90 |
for _, row in matches.head(5).iterrows():
|
| 91 |
results.append(f"**{row['name']}**: {', '.join(row['reactants'])} β {', '.join(row['products'])}")
|
| 92 |
return "\n\n".join(results)
|
| 93 |
+
# Fuzzy match for autocorrection
|
| 94 |
+
fuzzy_matches = process.extract(product, unique_products, limit=3)
|
| 95 |
+
if fuzzy_matches and fuzzy_matches[0][1] > 70:
|
| 96 |
+
closest = fuzzy_matches[0][0]
|
| 97 |
+
matches = df[df['products'].apply(lambda x: closest.lower() in [p.lower() for p in x])]
|
| 98 |
+
if not matches.empty:
|
| 99 |
+
results = [f"Did you mean '{closest}'?\n"]
|
| 100 |
+
for _, row in matches.head(5).iterrows():
|
| 101 |
+
results.append(f"**{row['name']}**: {', '.join(row['reactants'])} β {', '.join(row['products'])}")
|
| 102 |
+
return "\n\n".join(results)
|
| 103 |
return "No reactions found with that product."
|
| 104 |
|
| 105 |
def get_autocomplete_reactions(query):
|
|
|
|
| 125 |
gr.Markdown("Search through the organic reactions dataset by name, reactant, or product.")
|
| 126 |
|
| 127 |
with gr.Tab("Search by Reaction Name"):
|
| 128 |
+
reaction_input = gr.Dropdown(label="Reaction Name", choices=reaction_names, allow_custom_value=True, placeholder="Type or select a reaction name")
|
| 129 |
reaction_output = gr.Markdown(label="Result")
|
| 130 |
reaction_btn = gr.Button("Search")
|
| 131 |
reaction_btn.click(search_by_reaction_name, inputs=reaction_input, outputs=reaction_output)
|
| 132 |
|
| 133 |
with gr.Tab("Search by Reactant"):
|
| 134 |
+
reactant_input = gr.Dropdown(label="Reactant", choices=unique_reactants, allow_custom_value=True, placeholder="Type or select a reactant")
|
| 135 |
reactant_output = gr.Markdown(label="Results")
|
| 136 |
reactant_btn = gr.Button("Search")
|
| 137 |
reactant_btn.click(search_by_reactant, inputs=reactant_input, outputs=reactant_output)
|
| 138 |
|
| 139 |
with gr.Tab("View Reaction SVG"):
|
| 140 |
+
svg_input = gr.Dropdown(label="Reaction Name", choices=reaction_names, allow_custom_value=True, placeholder="Type or select a reaction name")
|
| 141 |
svg_output = gr.HTML(label="Reaction SVG")
|
| 142 |
svg_btn = gr.Button("Generate SVG")
|
| 143 |
svg_btn.click(generate_reaction_svg, inputs=svg_input, outputs=svg_output)
|
| 144 |
|
| 145 |
+
with gr.Tab("Search by Product"):
|
| 146 |
+
product_input = gr.Dropdown(label="Product", choices=unique_products, allow_custom_value=True, placeholder="Type or select a product")
|
| 147 |
+
product_output = gr.Markdown(label="Results")
|
| 148 |
+
product_btn = gr.Button("Search")
|
| 149 |
+
product_btn.click(search_by_product, inputs=product_input, outputs=product_output)
|
| 150 |
+
|
| 151 |
with gr.Tab("API Documentation"):
|
| 152 |
gr.Markdown("""
|
| 153 |
## API Endpoints
|