Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,109 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
# Function to
|
22 |
-
def
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
else:
|
58 |
-
st.write(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
if __name__ == "__main__":
|
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
|
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()
|