Update app.py
Browse files
app.py
CHANGED
@@ -6,8 +6,8 @@ import pandas as pd
|
|
6 |
import plotly.graph_objects as go
|
7 |
import plotly.express as px
|
8 |
import time
|
9 |
-
import random
|
10 |
from datetime import datetime, timedelta
|
|
|
11 |
|
12 |
# Custom CSS for styling
|
13 |
st.markdown("""
|
@@ -42,6 +42,22 @@ st.markdown("""
|
|
42 |
color: #777;
|
43 |
margin-top: 20px;
|
44 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
</style>
|
46 |
""", unsafe_allow_html=True)
|
47 |
|
@@ -74,6 +90,31 @@ def call_ai_model(all_message):
|
|
74 |
|
75 |
return response
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
# Function to generate simulated trust scores
|
78 |
def generate_trust_scores(technologies, issues):
|
79 |
trust_scores = {}
|
@@ -102,15 +143,6 @@ def generate_gender_impact(technologies, genders):
|
|
102 |
impact_scores[tech][gender] = random.uniform(-1, 1)
|
103 |
return impact_scores
|
104 |
|
105 |
-
# Function to generate simulated geographic usage data
|
106 |
-
def generate_geographic_data(technologies, regions):
|
107 |
-
geo_data = {}
|
108 |
-
for region in regions:
|
109 |
-
geo_data[region] = {}
|
110 |
-
for tech in technologies:
|
111 |
-
geo_data[region][tech] = random.uniform(0, 100)
|
112 |
-
return geo_data
|
113 |
-
|
114 |
# Function to generate simulated long-term economic impact
|
115 |
def generate_economic_impact(technologies):
|
116 |
impact_data = {}
|
@@ -153,7 +185,6 @@ if st.button("Generate Analysis"):
|
|
153 |
try:
|
154 |
stages = [
|
155 |
"Analyzing digital technologies...",
|
156 |
-
"Checking location data...",
|
157 |
"Running simulations...",
|
158 |
"Processing data...",
|
159 |
"Assessing impacts...",
|
@@ -163,131 +194,112 @@ if st.button("Generate Analysis"):
|
|
163 |
"Preparing output..."
|
164 |
]
|
165 |
|
166 |
-
|
167 |
-
|
168 |
-
st.spinner(stage)
|
169 |
-
time.sleep(2)
|
170 |
-
|
171 |
-
response = call_ai_model(all_message)
|
172 |
-
|
173 |
-
analysis_text = ""
|
174 |
-
for line in response.iter_lines():
|
175 |
-
if line:
|
176 |
-
line_content = line.decode('utf-8')
|
177 |
-
if line_content.startswith("data: "):
|
178 |
-
line_content = line_content[6:]
|
179 |
-
try:
|
180 |
-
json_data = json.loads(line_content)
|
181 |
-
if "choices" in json_data:
|
182 |
-
delta = json_data["choices"][0]["delta"]
|
183 |
-
if "content" in delta:
|
184 |
-
analysis_text += delta["content"]
|
185 |
-
except json.JSONDecodeError:
|
186 |
-
continue
|
187 |
-
|
188 |
-
st.success("Analysis completed!")
|
189 |
-
|
190 |
-
# Display analysis
|
191 |
-
st.markdown('<div class="section">', unsafe_allow_html=True)
|
192 |
-
st.subheader("Digital Technologies Impact Analysis in Kenya")
|
193 |
-
st.markdown(analysis_text.strip())
|
194 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
195 |
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
|
|
202 |
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
y=list(trust_scores[tech].values()),
|
211 |
-
name=tech
|
212 |
-
))
|
213 |
-
fig_trust.update_layout(barmode='group', title="Trust Scores by Technology and Issue")
|
214 |
-
st.plotly_chart(fig_trust)
|
215 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
216 |
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
fig_kinship.add_trace(go.Scatterpolar(
|
223 |
-
r=list(kinship_impact[tech].values()),
|
224 |
-
theta=list(kinship_impact[tech].keys()),
|
225 |
-
fill='toself',
|
226 |
-
name=tech
|
227 |
-
))
|
228 |
-
fig_kinship.update_layout(polar=dict(radialaxis=dict(visible=True, range=[-1, 1])), showlegend=True)
|
229 |
-
st.plotly_chart(fig_kinship)
|
230 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
231 |
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
245 |
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
|
|
260 |
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
|
|
|
|
277 |
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
|
292 |
except ValueError as ve:
|
293 |
st.error(f"Configuration error: {ve}")
|
|
|
6 |
import plotly.graph_objects as go
|
7 |
import plotly.express as px
|
8 |
import time
|
|
|
9 |
from datetime import datetime, timedelta
|
10 |
+
import random
|
11 |
|
12 |
# Custom CSS for styling
|
13 |
st.markdown("""
|
|
|
42 |
color: #777;
|
43 |
margin-top: 20px;
|
44 |
}
|
45 |
+
.stProgress > div > div > div > div {
|
46 |
+
background-image: linear-gradient(to right, #4CAF50, #45a049);
|
47 |
+
}
|
48 |
+
@keyframes gradient {
|
49 |
+
0% {background-position: 0% 50%;}
|
50 |
+
50% {background-position: 100% 50%;}
|
51 |
+
100% {background-position: 0% 50%;}
|
52 |
+
}
|
53 |
+
.animated-div {
|
54 |
+
background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
|
55 |
+
background-size: 400% 400%;
|
56 |
+
animation: gradient 15s ease infinite;
|
57 |
+
padding: 10px;
|
58 |
+
border-radius: 5px;
|
59 |
+
margin-bottom: 10px;
|
60 |
+
}
|
61 |
</style>
|
62 |
""", unsafe_allow_html=True)
|
63 |
|
|
|
90 |
|
91 |
return response
|
92 |
|
93 |
+
# Function to process AI response
|
94 |
+
def process_ai_response(response):
|
95 |
+
explanation_text = ""
|
96 |
+
for line in response.iter_lines():
|
97 |
+
if line:
|
98 |
+
line_content = line.decode('utf-8')
|
99 |
+
if line_content.startswith("data: "):
|
100 |
+
line_content = line_content[6:]
|
101 |
+
try:
|
102 |
+
json_data = json.loads(line_content)
|
103 |
+
if "choices" in json_data:
|
104 |
+
delta = json_data["choices"][0]["delta"]
|
105 |
+
if "content" in delta:
|
106 |
+
explanation_text += delta["content"]
|
107 |
+
except json.JSONDecodeError:
|
108 |
+
continue
|
109 |
+
return explanation_text.strip()
|
110 |
+
|
111 |
+
# Function to get AI explanation for graphs
|
112 |
+
def get_ai_explanation(graph_type, data):
|
113 |
+
explanation_prompt = f"Provide a short, clear explanation of the following {graph_type} graph data: {data}"
|
114 |
+
response = call_ai_model(explanation_prompt)
|
115 |
+
explanation = process_ai_response(response)
|
116 |
+
return explanation
|
117 |
+
|
118 |
# Function to generate simulated trust scores
|
119 |
def generate_trust_scores(technologies, issues):
|
120 |
trust_scores = {}
|
|
|
143 |
impact_scores[tech][gender] = random.uniform(-1, 1)
|
144 |
return impact_scores
|
145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
# Function to generate simulated long-term economic impact
|
147 |
def generate_economic_impact(technologies):
|
148 |
impact_data = {}
|
|
|
185 |
try:
|
186 |
stages = [
|
187 |
"Analyzing digital technologies...",
|
|
|
188 |
"Running simulations...",
|
189 |
"Processing data...",
|
190 |
"Assessing impacts...",
|
|
|
194 |
"Preparing output..."
|
195 |
]
|
196 |
|
197 |
+
progress_bar = st.progress(0)
|
198 |
+
status_text = st.empty()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
|
200 |
+
for i, stage in enumerate(stages):
|
201 |
+
status_text.markdown(f'<div class="animated-div">{stage}</div>', unsafe_allow_html=True)
|
202 |
+
progress_bar.progress((i + 1) / len(stages))
|
203 |
+
time.sleep(1)
|
204 |
+
|
205 |
+
response = call_ai_model(all_message)
|
206 |
+
analysis_text = process_ai_response(response)
|
207 |
|
208 |
+
st.success("Analysis completed!")
|
209 |
+
|
210 |
+
# Display analysis
|
211 |
+
st.markdown('<div class="section">', unsafe_allow_html=True)
|
212 |
+
st.subheader("Digital Technologies Impact Analysis in Kenya")
|
213 |
+
st.markdown(analysis_text)
|
214 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
|
216 |
+
# Generate simulated data
|
217 |
+
trust_scores = generate_trust_scores(digital_technologies, issues)
|
218 |
+
kinship_impact = generate_kinship_impact(digital_technologies)
|
219 |
+
gender_impact = generate_gender_impact(digital_technologies, gender_focus)
|
220 |
+
economic_impact = generate_economic_impact(digital_technologies)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
|
222 |
+
# Trust and Fraud Metrics Visualization
|
223 |
+
st.markdown('<div class="section">', unsafe_allow_html=True)
|
224 |
+
st.subheader("Trust and Fraud Metrics")
|
225 |
+
fig_trust = go.Figure()
|
226 |
+
for tech in digital_technologies:
|
227 |
+
fig_trust.add_trace(go.Bar(
|
228 |
+
x=list(trust_scores[tech].keys()),
|
229 |
+
y=list(trust_scores[tech].values()),
|
230 |
+
name=tech
|
231 |
+
))
|
232 |
+
fig_trust.update_layout(barmode='group', title="Trust Scores by Technology and Issue")
|
233 |
+
st.plotly_chart(fig_trust)
|
234 |
+
trust_explanation = get_ai_explanation("Trust and Fraud Metrics", trust_scores)
|
235 |
+
st.markdown(f"**AI Explanation:** {trust_explanation}")
|
236 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
237 |
+
|
238 |
+
# Kinship Structure Analysis
|
239 |
+
st.markdown('<div class="section">', unsafe_allow_html=True)
|
240 |
+
st.subheader("Impact on Kinship Structures")
|
241 |
+
fig_kinship = go.Figure()
|
242 |
+
for tech in digital_technologies:
|
243 |
+
fig_kinship.add_trace(go.Scatterpolar(
|
244 |
+
r=list(kinship_impact[tech].values()),
|
245 |
+
theta=list(kinship_impact[tech].keys()),
|
246 |
+
fill='toself',
|
247 |
+
name=tech
|
248 |
+
))
|
249 |
+
fig_kinship.update_layout(polar=dict(radialaxis=dict(visible=True, range=[-1, 1])), showlegend=True)
|
250 |
+
st.plotly_chart(fig_kinship)
|
251 |
+
kinship_explanation = get_ai_explanation("Impact on Kinship Structures", kinship_impact)
|
252 |
+
st.markdown(f"**AI Explanation:** {kinship_explanation}")
|
253 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
254 |
|
255 |
+
# Gender Impact Visualization
|
256 |
+
st.markdown('<div class="section">', unsafe_allow_html=True)
|
257 |
+
st.subheader("Gender Impact Analysis")
|
258 |
+
fig_gender = go.Figure()
|
259 |
+
for tech in digital_technologies:
|
260 |
+
fig_gender.add_trace(go.Bar(
|
261 |
+
x=list(gender_impact[tech].keys()),
|
262 |
+
y=list(gender_impact[tech].values()),
|
263 |
+
name=tech
|
264 |
+
))
|
265 |
+
fig_gender.update_layout(barmode='group', title="Gender Impact by Technology")
|
266 |
+
st.plotly_chart(fig_gender)
|
267 |
+
gender_explanation = get_ai_explanation("Gender Impact Analysis", gender_impact)
|
268 |
+
st.markdown(f"**AI Explanation:** {gender_explanation}")
|
269 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
270 |
|
271 |
+
# Long-term Economic Impact
|
272 |
+
st.markdown('<div class="section">', unsafe_allow_html=True)
|
273 |
+
st.subheader("Projected Long-term Economic Impact")
|
274 |
+
fig_economic = go.Figure()
|
275 |
+
years = [datetime.now().year + i for i in range(5)]
|
276 |
+
for tech in digital_technologies:
|
277 |
+
for indicator in economic_impact[tech]:
|
278 |
+
fig_economic.add_trace(go.Scatter(
|
279 |
+
x=years,
|
280 |
+
y=economic_impact[tech][indicator],
|
281 |
+
mode='lines+markers',
|
282 |
+
name=f"{tech} - {indicator}"
|
283 |
+
))
|
284 |
+
fig_economic.update_layout(title="5-Year Economic Impact Projection", xaxis_title="Year", yaxis_title="Impact (%)")
|
285 |
+
st.plotly_chart(fig_economic)
|
286 |
+
economic_explanation = get_ai_explanation("Projected Long-term Economic Impact", economic_impact)
|
287 |
+
st.markdown(f"**AI Explanation:** {economic_explanation}")
|
288 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
289 |
|
290 |
+
# Ethical Considerations
|
291 |
+
st.markdown('<div class="section">', unsafe_allow_html=True)
|
292 |
+
st.subheader("Ethical Considerations")
|
293 |
+
ethical_concerns = [
|
294 |
+
"Data Privacy: Ensuring user data is protected and used responsibly.",
|
295 |
+
"Digital Divide: Addressing inequality in access to digital technologies.",
|
296 |
+
"Cultural Preservation: Balancing technological advancement with traditional values.",
|
297 |
+
"Algorithmic Bias: Mitigating biases in AI and machine learning systems.",
|
298 |
+
"Cybersecurity: Protecting users from fraud and cyber attacks"
|
299 |
+
]
|
300 |
+
for concern in ethical_concerns:
|
301 |
+
st.write(f"• {concern}")
|
302 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
303 |
|
304 |
except ValueError as ve:
|
305 |
st.error(f"Configuration error: {ve}")
|