shukdevdatta123 commited on
Commit
37dee58
·
verified ·
1 Parent(s): 4b51f20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -56
app.py CHANGED
@@ -1,61 +1,109 @@
 
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
- def main():
31
- st.title("Dhaka Local Buses and Routes Finder")
32
-
33
- # Bus Name Search Section
34
- st.header("Search Bus Name")
35
-
36
- # Add a placeholder as the first item in the bus list
37
- bus_name_options = ['Select Bus Name from dropdown'] + df['Dhaka Local Buses'].tolist()
38
-
39
- bus_name = st.selectbox('Select a Bus Name', bus_name_options)
40
-
41
- if bus_name != 'Select Bus Name from dropdown':
42
- route = get_route_by_bus(bus_name)
43
- st.write(f"Routes for **{bus_name}**:")
44
- st.write(route)
45
- else:
46
- st.write("Please select a bus name from the dropdown to view its 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()
 
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
36
+ def generate_creative_content(prompt):
37
+ try:
38
+ response = openai.Completion.create(
39
+ model="gpt-3.5-turbo", # Or "gpt-3.5-turbo" for more creativity
40
+ prompt=prompt,
41
+ max_tokens=150,
42
+ n=1,
43
+ stop=None,
44
+ temperature=0.7 # You can adjust temperature for more creative results
45
+ )
46
+ return response.choices[0].text.strip()
47
+ except Exception as e:
48
+ return f"Error: {str(e)}"
49
+
50
+ # Adding GPT-3.5 functionality to your app
51
+ def main():
52
+ st.title("Dhaka Local Buses and Routes Finder with GPT Creativity")
53
+
54
+ # Fun Bus-Related Facts or Trivia Section
55
+ st.header("Ask GPT for Fun Bus Facts or Trivia!")
56
+ trivia_input = st.text_input("Ask a fun fact or trivia about Dhaka's local buses:")
57
+ if trivia_input:
58
+ trivia_response = generate_creative_content(f"Tell me an interesting fact or trivia about Dhaka's local buses.")
59
+ st.write(trivia_response)
60
+
61
+ # Route Optimization Suggestions Section
62
+ st.header("Route Optimization Suggestions")
63
+ start_location = st.text_input("Enter your starting location (e.g., Mirpur):")
64
+ destination_location = st.text_input("Enter your destination location (e.g., Dhaka University):")
65
+ if start_location and destination_location:
66
+ route_suggestion_prompt = f"Suggest the best bus route from {start_location} to {destination_location}, considering speed, convenience, and the most popular routes."
67
+ route_suggestion = generate_creative_content(route_suggestion_prompt)
68
+ st.write(route_suggestion)
69
+
70
+ # Bus-Themed Storytelling Section
71
+ st.header("Create a Bus-Themed Story!")
72
+ story_input = st.text_input("Enter a location (e.g., Mirpur to Gabtoli) to create a bus-themed story:")
73
+ if story_input:
74
+ story_prompt = f"Create a short story about a passenger taking a bus from {story_input}."
75
+ bus_story = generate_creative_content(story_prompt)
76
+ st.write(bus_story)
77
+
78
+ # General Questions about Local Buses
79
+ st.header("Ask General Questions About Dhaka's Local Buses")
80
+ question_input = st.text_input("Ask a question about the Dhaka bus system (e.g., most common issues faced by commuters):")
81
+ if question_input:
82
+ question_answer = generate_creative_content(f"Answer the question: {question_input} about Dhaka's local buses.")
83
+ st.write(question_answer)
84
+
85
+ # Existing bus search functionality (unchanged)
86
+ st.header("Search Bus Name")
87
+ bus_name_options = ['Select Bus Name from dropdown'] + df['Dhaka Local Buses'].tolist()
88
+ bus_name = st.selectbox('Select a Bus Name', bus_name_options)
89
+ if bus_name != 'Select Bus Name from dropdown':
90
+ route = get_route_by_bus(bus_name)
91
+ st.write(f"Routes for **{bus_name}**:")
92
+ st.write(route)
93
  else:
94
+ st.write("Please select a bus name from the dropdown to view its route.")
95
+
96
+ # Location search functionality remains as before
97
+ st.header("Search Location")
98
+ location = st.text_input('Enter a location (e.g., Gabtoli, Mirpur)', '').strip()
99
+ if location:
100
+ buses = search_by_location(location)
101
+ if not buses.empty:
102
+ st.write(f"Buses passing through **{location}**:")
103
+ for idx, row in buses.iterrows():
104
+ st.write(f"- {row['Dhaka Local Buses']}: {clean_route(row['Routes'])}")
105
+ else:
106
+ st.write(f"No buses found for the location '**{location}**.")
107
 
108
+ if __name__ == "__main__":
109
+ main()