Spaces:
Runtime error
Runtime error
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
from fuzzywuzzy import process
|
4 |
+
import requests
|
5 |
+
|
6 |
+
url = "https://csrlc421nb.execute-api.us-east-2.amazonaws.com/dev/predict"
|
7 |
+
|
8 |
+
|
9 |
+
# Load stations data from the JSON file
|
10 |
+
with open('stations.json') as f:
|
11 |
+
stations_data = json.load(f)
|
12 |
+
|
13 |
+
stations = stations_data['stations']
|
14 |
+
|
15 |
+
# Create a mapping from station name to station ID
|
16 |
+
station_name_to_id = {station['name']: station['station_id'] for station in stations}
|
17 |
+
|
18 |
+
# Define a function to find the closest matching station
|
19 |
+
def find_station(user_input):
|
20 |
+
# Use fuzzy matching to find the top 3 closest station names
|
21 |
+
matches = process.extract(user_input, station_name_to_id.keys(), limit=3)
|
22 |
+
|
23 |
+
# If the best match has a score of 90 or above, we assume it's a good match
|
24 |
+
if matches[0][1] >= 90:
|
25 |
+
best_match = matches[0][0]
|
26 |
+
station_id = station_name_to_id[best_match]
|
27 |
+
|
28 |
+
response = requests.get(url, params={"station_id": station_id})
|
29 |
+
|
30 |
+
return response.json()
|
31 |
+
|
32 |
+
# If ambiguous, return suggestions
|
33 |
+
suggestions = [match[0] for match in matches]
|
34 |
+
return f"Did you mean one of these stations? {', '.join(suggestions)}"
|
35 |
+
|
36 |
+
# Create Gradio interface
|
37 |
+
station_input = gr.Textbox(label="Enter Station Name") # Use gr.Textbox for input
|
38 |
+
output_text = gr.Textbox(label="Station ID or Suggestions") # Use gr.Textbox for output
|
39 |
+
|
40 |
+
# Launch the Gradio interface
|
41 |
+
gr.Interface(fn=find_station, inputs=station_input, outputs=output_text, title="Station Finder").launch()
|