Update app.py
#1
by
bigdog54
- opened
app.py
CHANGED
@@ -1,34 +1,19 @@
|
|
1 |
import streamlit as st
|
2 |
import openai
|
3 |
-
import os
|
4 |
-
import urllib.parse
|
5 |
|
6 |
# Ensure your OpenAI API key is set in your environment variables
|
7 |
openai.api_key = os.environ["OPENAI_API_KEY"]
|
8 |
|
9 |
-
# Mapping of state names to abbreviations
|
10 |
-
STATE_ABBREVIATIONS = {
|
11 |
-
'alabama': 'al', 'alaska': 'ak', 'arizona': 'az', 'arkansas': 'ar', 'california': 'ca',
|
12 |
-
'colorado': 'co', 'connecticut': 'ct', 'delaware': 'de', 'florida': 'fl', 'georgia': 'ga',
|
13 |
-
'hawaii': 'hi', 'idaho': 'id', 'illinois': 'il', 'indiana': 'in', 'iowa': 'ia',
|
14 |
-
'kansas': 'ks', 'kentucky': 'ky', 'louisiana': 'la', 'maine': 'me', 'maryland': 'md',
|
15 |
-
'massachusetts': 'ma', 'michigan': 'mi', 'minnesota': 'mn', 'mississippi': 'ms', 'missouri': 'mo',
|
16 |
-
'montana': 'mt', 'nebraska': 'ne', 'nevada': 'nv', 'new hampshire': 'nh', 'new jersey': 'nj',
|
17 |
-
'new mexico': 'nm', 'new york': 'ny', 'north carolina': 'nc', 'north dakota': 'nd',
|
18 |
-
'ohio': 'oh', 'oklahoma': 'ok', 'oregon': 'or', 'pennsylvania': 'pa', 'rhode island': 'ri',
|
19 |
-
'south carolina': 'sc', 'south dakota': 'sd', 'tennessee': 'tn', 'texas': 'tx',
|
20 |
-
'utah': 'ut', 'vermont': 'vt', 'virginia': 'va', 'washington': 'wa', 'west virginia': 'wv',
|
21 |
-
'wisconsin': 'wi', 'wyoming': 'wy'
|
22 |
-
}
|
23 |
-
|
24 |
initial_messages = [{
|
25 |
"role": "system",
|
26 |
"content": """
|
27 |
-
You are a
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
32 |
"""
|
33 |
}]
|
34 |
|
@@ -36,64 +21,17 @@ def call_openai_api(messages):
|
|
36 |
return openai.ChatCompletion.create(
|
37 |
model="gpt-3.5-turbo",
|
38 |
messages=messages,
|
39 |
-
max_tokens=
|
40 |
)
|
41 |
|
42 |
-
def
|
43 |
-
"""
|
44 |
-
Parse the response text to extract neighborhood information.
|
45 |
-
Returns a list of dictionaries containing neighborhood info.
|
46 |
-
"""
|
47 |
-
neighborhoods = []
|
48 |
-
lines = response_text.strip().split('\n')
|
49 |
-
for line in lines:
|
50 |
-
line = line.strip()
|
51 |
-
if line and (line.startswith('1.') or line.startswith('2.') or line.startswith('3.')):
|
52 |
-
if ':' in line:
|
53 |
-
location_parts = line.split(':', 1)[0].split(',')
|
54 |
-
if len(location_parts) >= 3:
|
55 |
-
neighborhood = location_parts[0].split('.', 1)[1].strip()
|
56 |
-
city = location_parts[1].strip()
|
57 |
-
state = location_parts[2].strip()
|
58 |
-
neighborhoods.append({
|
59 |
-
'neighborhood': neighborhood,
|
60 |
-
'city': city,
|
61 |
-
'state': state,
|
62 |
-
'full': f"{neighborhood}, {city}, {state}"
|
63 |
-
})
|
64 |
-
return neighborhoods
|
65 |
-
|
66 |
-
def format_zillow_search(neighborhood_info):
|
67 |
-
"""
|
68 |
-
Format the location string for Zillow search using 'neighborhood, city, state' format with commas and _rb at the end.
|
69 |
-
Fallback to a city-level search if the neighborhood is not recognized.
|
70 |
-
"""
|
71 |
-
# Prepare each component with correct capitalization and format for Zillow
|
72 |
-
neighborhood = urllib.parse.quote(neighborhood_info['neighborhood'])
|
73 |
-
city = urllib.parse.quote(neighborhood_info['city'])
|
74 |
-
|
75 |
-
# Convert full state name to abbreviation if needed
|
76 |
-
state = neighborhood_info['state'].lower()
|
77 |
-
state_abbr = STATE_ABBREVIATIONS.get(state, state[:2]).upper() # Ensure state is uppercase
|
78 |
-
|
79 |
-
# Primary search path with neighborhood
|
80 |
-
search_path = f"{neighborhood},-{city},-{state_abbr}_rb"
|
81 |
-
url = f"https://www.zillow.com/homes/{search_path}/"
|
82 |
-
|
83 |
-
# Check URL - fallback to city-level URL if Zillow cannot interpret it
|
84 |
-
if not neighborhood: # If neighborhood is empty or unrecognized, return a city-level search URL
|
85 |
-
search_path = f"{city},-{state_abbr}_rb"
|
86 |
-
url = f"https://www.zillow.com/homes/{search_path}/"
|
87 |
-
|
88 |
-
return url
|
89 |
-
|
90 |
-
def CustomChatGPT(city, preferences, messages):
|
91 |
query = f"""
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
97 |
"""
|
98 |
|
99 |
messages.append({"role": "user", "content": query})
|
@@ -110,34 +48,29 @@ if "reply" not in st.session_state:
|
|
110 |
st.session_state["reply"] = None
|
111 |
|
112 |
# Centered title
|
113 |
-
st.markdown("<h1 style='text-align: center; color: black;'>
|
114 |
|
115 |
# User inputs
|
116 |
col1, col2 = st.columns(2)
|
117 |
with col1:
|
118 |
-
st.markdown("<h2 style='text-align: center; color: black;'>Your
|
119 |
-
|
120 |
-
|
121 |
-
generate_button = st.button('
|
122 |
|
123 |
# Process results on button click
|
124 |
-
if generate_button and
|
125 |
messages = initial_messages.copy()
|
126 |
-
st.session_state["reply"], _ = CustomChatGPT(
|
127 |
|
128 |
# Display results if there is a reply in session state
|
129 |
if st.session_state["reply"]:
|
130 |
with col2:
|
131 |
-
st.markdown("<h2 style='text-align: center; color: black;'>
|
132 |
st.write(st.session_state["reply"])
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
for hood in neighborhoods:
|
140 |
-
zillow_url = format_zillow_search(hood)
|
141 |
-
st.markdown(f"- [Search homes in {hood['neighborhood']}, {hood['city']}, {hood['state']}]({zillow_url})")
|
142 |
-
else:
|
143 |
-
st.warning("Unable to generate Zillow links. Please try again.")
|
|
|
1 |
import streamlit as st
|
2 |
import openai
|
3 |
+
import os
|
|
|
4 |
|
5 |
# Ensure your OpenAI API key is set in your environment variables
|
6 |
openai.api_key = os.environ["OPENAI_API_KEY"]
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
initial_messages = [{
|
9 |
"role": "system",
|
10 |
"content": """
|
11 |
+
You are a time management and self-discipline coach for students. Your task is to create
|
12 |
+
a personalized plan for students who struggle with managing their time and maintaining discipline.
|
13 |
+
For each response:
|
14 |
+
1. Divide the plan into three main sections: Study Routine, Personal Life, and Discipline Tips.
|
15 |
+
2. Provide detailed and actionable steps for each section.
|
16 |
+
3. Keep the tone positive, encouraging, and practical.
|
17 |
"""
|
18 |
}]
|
19 |
|
|
|
21 |
return openai.ChatCompletion.create(
|
22 |
model="gpt-3.5-turbo",
|
23 |
messages=messages,
|
24 |
+
max_tokens=500
|
25 |
)
|
26 |
|
27 |
+
def CustomChatGPT(student_goals, challenges, messages):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
query = f"""
|
29 |
+
A student is struggling with the following challenges: {challenges}.
|
30 |
+
Their goals are: {student_goals}.
|
31 |
+
Please create a personalized time management and self-discipline plan:
|
32 |
+
1. Include a Study Routine section with daily and weekly schedules.
|
33 |
+
2. Include a Personal Life section with advice on managing personal responsibilities and relaxation.
|
34 |
+
3. Provide Discipline Tips with motivational strategies and tools to stay on track.
|
35 |
"""
|
36 |
|
37 |
messages.append({"role": "user", "content": query})
|
|
|
48 |
st.session_state["reply"] = None
|
49 |
|
50 |
# Centered title
|
51 |
+
st.markdown("<h1 style='text-align: center; color: black;'>Student Self-Discipline and Time Management Assistant</h1>", unsafe_allow_html=True)
|
52 |
|
53 |
# User inputs
|
54 |
col1, col2 = st.columns(2)
|
55 |
with col1:
|
56 |
+
st.markdown("<h2 style='text-align: center; color: black;'>Your Situation</h2>", unsafe_allow_html=True)
|
57 |
+
student_goals = st.text_area("What are your goals?", placeholder="E.g., improve grades, balance studies and personal life, finish assignments on time")
|
58 |
+
challenges = st.text_area("What challenges are you facing?", placeholder="E.g., procrastination, lack of focus, poor time management")
|
59 |
+
generate_button = st.button('Get My Plan')
|
60 |
|
61 |
# Process results on button click
|
62 |
+
if generate_button and student_goals and challenges:
|
63 |
messages = initial_messages.copy()
|
64 |
+
st.session_state["reply"], _ = CustomChatGPT(student_goals, challenges, messages)
|
65 |
|
66 |
# Display results if there is a reply in session state
|
67 |
if st.session_state["reply"]:
|
68 |
with col2:
|
69 |
+
st.markdown("<h2 style='text-align: center; color: black;'>Your Personalized Plan ⬇️</h2>", unsafe_allow_html=True)
|
70 |
st.write(st.session_state["reply"])
|
71 |
+
|
72 |
+
# Additional resources and tips
|
73 |
+
st.markdown("### 📚 Additional Resources")
|
74 |
+
st.markdown("- [Pomodoro Technique for Time Management](https://en.wikipedia.org/wiki/Pomodoro_Technique)")
|
75 |
+
st.markdown("- [Free Study Schedule Templates](https://www.smartsheet.com/free-study-schedule-templates)")
|
76 |
+
st.markdown("- [Mindfulness Tips for Students](https://www.mindful.org/mindfulness-for-students/)")
|
|
|
|
|
|
|
|
|
|