shukdevdatta123 commited on
Commit
c922e1e
·
verified ·
1 Parent(s): 4f634bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -120
app.py CHANGED
@@ -1,125 +1,61 @@
1
- import openai
2
  import streamlit as st
3
  import pandas as pd
4
 
5
- # Prompt user to enter OpenAI API key
6
- openai.api_key = st.text_input("Enter your OpenAI API Key", type="password")
7
-
8
- # Ensure the API key is entered before continuing
9
- if not openai.api_key:
10
- st.warning("Please enter your OpenAI API key to use the app.")
11
- else:
12
- # Load the Excel file into a DataFrame (adjust the file path accordingly)
13
- df = pd.read_excel('Book1.xlsx')
14
-
15
- # Clean up the DataFrame (strip any unnecessary whitespaces in column names)
16
- df.columns = df.columns.str.strip()
17
-
18
- # Function to clean routes and remove unwanted characters
19
- def clean_route(route):
20
- return route.replace("_x000D_", "").strip()
21
-
22
- # Function to search buses by location
23
- def search_by_location(location):
24
- buses_with_location = df[df['Routes'].str.contains(location, case=False, na=False)]
25
- return buses_with_location
26
-
27
- # Function to get the route of a selected bus
28
- def get_route_by_bus(bus_name):
29
- bus_route = df[df['Dhaka Local Buses'] == bus_name]['Routes'].values
30
- if len(bus_route) > 0:
31
- return clean_route(bus_route[0]) # Clean the route before returning
32
- else:
33
- return "No route found for this bus."
34
-
35
- # Function to generate creative content using GPT-3.5 (chat version)
36
- def generate_creative_content(prompt):
37
- try:
38
- response = openai.ChatCompletion.create(
39
- model="gpt-3.5-turbo", # Ensure using the chat model
40
- messages=[{"role": "system", "content": "You are a helpful assistant."},
41
- {"role": "user", "content": prompt}],
42
- max_tokens=150,
43
- temperature=0.7 # You can adjust temperature for more creative results
44
- )
45
- return response['choices'][0]['message']['content'].strip()
46
- except Exception as e:
47
- return f"Error: {str(e)}"
48
-
49
- # Adding this function to find buses between two locations
50
- def get_buses_between_locations(start_location, destination_location):
51
- # Filter buses that pass through both the start and destination locations
52
- buses_start = df[df['Routes'].str.contains(start_location, case=False, na=False)]
53
- buses_destination = df[df['Routes'].str.contains(destination_location, case=False, na=False)]
54
-
55
- # Find buses that are common in both filtered dataframes
56
- common_buses = pd.merge(buses_start, buses_destination, on='Dhaka Local Buses')
57
-
58
- return common_buses
59
-
60
- # Main Streamlit function
61
- def main():
62
- st.title("Dhaka Local Buses and Routes Finder with GPT Creativity")
63
-
64
- # Fun Bus-Related Facts or Trivia Section
65
- st.header("Ask GPT for Fun Bus Facts or Trivia!")
66
- trivia_input = st.text_input("Ask a fun fact or trivia about Dhaka's local buses:")
67
- if trivia_input:
68
- trivia_response = generate_creative_content(f"Tell me an interesting fact or trivia about Dhaka's local buses.")
69
- st.write(trivia_response)
70
-
71
- # Route Optimization Suggestions Section
72
- st.header("Route Optimization Suggestions")
73
- start_location = st.text_input("Enter your starting location (e.g., Mirpur):")
74
- destination_location = st.text_input("Enter your destination location (e.g., Dhaka University):")
75
- if start_location and destination_location:
76
- # Get buses that pass through both the start and destination locations
77
- buses_with_common_routes = get_buses_between_locations(start_location, destination_location)
78
-
79
- if not buses_with_common_routes.empty:
80
- st.write(f"Suggested buses from **{start_location}** to **{destination_location}**:")
81
- for idx, row in buses_with_common_routes.iterrows():
82
- st.write(f"- **{row['Dhaka Local Buses']}**: {clean_route(row['Routes'])}")
83
- else:
84
- st.write(f"No buses found passing through both **{start_location}** and **{destination_location}**.")
85
-
86
- # Bus-Themed Storytelling Section
87
- st.header("Create a Bus-Themed Story!")
88
- story_input = st.text_input("Enter a location (e.g., Mirpur to Gabtoli) to create a bus-themed story:")
89
- if story_input:
90
- story_prompt = f"Create a short story about a passenger taking a bus from {story_input}."
91
- bus_story = generate_creative_content(story_prompt)
92
- st.write(bus_story)
93
-
94
- # General Questions about Local Buses
95
- st.header("Ask General Questions About Dhaka's Local Buses")
96
- question_input = st.text_input("Ask a question about the Dhaka bus system (e.g., most common issues faced by commuters):")
97
- if question_input:
98
- question_answer = generate_creative_content(f"Answer the question: {question_input} about Dhaka's local buses.")
99
- st.write(question_answer)
100
-
101
- # Existing bus search functionality (unchanged)
102
- st.header("Search Bus Name")
103
- bus_name_options = ['Select Bus Name from dropdown'] + df['Dhaka Local Buses'].tolist()
104
- bus_name = st.selectbox('Select a Bus Name', bus_name_options)
105
- if bus_name != 'Select Bus Name from dropdown':
106
- route = get_route_by_bus(bus_name)
107
- st.write(f"Routes for **{bus_name}**:")
108
- st.write(route)
109
  else:
110
- st.write("Please select a bus name from the dropdown to view its route.")
111
-
112
- # Location search functionality remains as before
113
- st.header("Search Location")
114
- location = st.text_input('Enter a location (e.g., Gabtoli, Mirpur)', '').strip()
115
- if location:
116
- buses = search_by_location(location)
117
- if not buses.empty:
118
- st.write(f"Buses passing through **{location}**:")
119
- for idx, row in buses.iterrows():
120
- st.write(f"- {row['Dhaka Local Buses']}: {clean_route(row['Routes'])}")
121
- else:
122
- st.write(f"No buses found for the location '**{location}**.")
123
 
124
- if __name__ == "__main__":
125
- main()
 
 
1
  import streamlit as st
2
  import pandas as pd
3
 
4
+ # Load the Excel file into a DataFrame (adjust the file path accordingly)
5
+ df = pd.read_excel('Book1.xlsx')
6
+
7
+ # Clean up the DataFrame (strip any unnecessary whitespaces in column names)
8
+ df.columns = df.columns.str.strip()
9
+
10
+ # Function to clean routes and remove unwanted characters
11
+ def clean_route(route):
12
+ # Remove the "_x000D_" carriage return issue and other unwanted characters
13
+ return route.replace("_x000D_", "").strip()
14
+
15
+ # Function to search buses by location
16
+ def search_by_location(location):
17
+ # Filter buses that have the location in their routes
18
+ buses_with_location = df[df['Routes'].str.contains(location, case=False, na=False)]
19
+ return buses_with_location
20
+
21
+ # Function to get the route of a selected bus
22
+ def get_route_by_bus(bus_name):
23
+ # Get the route for the selected bus
24
+ bus_route = df[df['Dhaka Local Buses'] == bus_name]['Routes'].values
25
+ if len(bus_route) > 0:
26
+ return clean_route(bus_route[0]) # Clean the route before returning
27
+ else:
28
+ return "No route found for this bus."
29
+
30
+ # Streamlit app
31
+ def main():
32
+ st.title("Dhaka Local Buses and Routes Finder")
33
+
34
+ # Bus Name Search Section
35
+ st.header("Search Bus Name")
36
+
37
+ # Add a placeholder as the default option for the selectbox
38
+ bus_name = st.selectbox(
39
+ 'Select a Bus Name from dropdown',
40
+ ['Select Bus Name from dropdown'] + df['Dhaka Local Buses'].tolist() # Adding the placeholder at the beginning
41
+ )
42
+
43
+ if bus_name != 'Select Bus Name from dropdown': # Proceed only if a valid bus is selected
44
+ route = get_route_by_bus(bus_name)
45
+ st.write(f"Routes for **{bus_name}**:")
46
+ st.write(route)
47
+
48
+ # Location Search Section
49
+ st.header("Search Location")
50
+ location = st.text_input('Enter a location (e.g., Gabtoli, Mirpur)', '').strip()
51
+ if location:
52
+ buses = search_by_location(location)
53
+ if not buses.empty:
54
+ st.write(f"Buses passing through **{location}**:")
55
+ for idx, row in buses.iterrows():
56
+ st.write(f"- {row['Dhaka Local Buses']}: {clean_route(row['Routes'])}") # Clean routes before displaying
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  else:
58
+ st.write(f"No buses found for the location '**{location}**'.")
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
+ if __name__ == "__main__":
61
+ main()