Update app.py
Browse files
app.py
CHANGED
@@ -2,77 +2,40 @@ import streamlit as st
|
|
2 |
import pandas as pd
|
3 |
import time
|
4 |
import matplotlib.pyplot as plt
|
|
|
5 |
|
6 |
-
#
|
7 |
-
def
|
8 |
-
|
9 |
-
|
10 |
-
return new_df
|
11 |
|
12 |
-
#
|
13 |
def vote_story(index):
|
14 |
st.session_state.df.loc[index, 'votes'] += 1
|
15 |
-
st.session_state.df =
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
return story.split(". ")
|
20 |
|
21 |
-
#
|
22 |
-
def extract_high_info_words(sentence):
|
23 |
-
words = sentence.split(" ")
|
24 |
-
return [word for word in words if len(word) > 6]
|
25 |
-
|
26 |
-
# Function to display story and voting
|
27 |
-
def display_story(index, row):
|
28 |
-
sentences = break_into_sentences(row['story'])
|
29 |
-
for sentence in sentences:
|
30 |
-
st.text(sentence)
|
31 |
-
|
32 |
-
high_info_words = []
|
33 |
-
for sentence in sentences:
|
34 |
-
high_info_words.extend(extract_high_info_words(sentence))
|
35 |
-
|
36 |
-
if st.button(f"Vote for: {' '.join(high_info_words)}"):
|
37 |
-
vote_story(index)
|
38 |
-
|
39 |
-
# Initialize session state for the vote counts
|
40 |
if 'df' not in st.session_state:
|
41 |
try:
|
42 |
st.session_state.df = pd.read_csv('votes.csv')
|
43 |
except FileNotFoundError:
|
44 |
-
st.session_state.df = pd.DataFrame({
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
# Display total votes
|
62 |
-
st.markdown("### 📊 Vote Summary")
|
63 |
-
st.table(st.session_state.df)
|
64 |
-
|
65 |
-
# Show all votes in one graph
|
66 |
-
fig, ax = plt.subplots()
|
67 |
-
ax.barh(st.session_state.df.index, st.session_state.df['votes'])
|
68 |
-
ax.set_yticks(st.session_state.df.index)
|
69 |
-
ax.set_yticklabels(st.session_state.df['story'])
|
70 |
-
st.pyplot(fig)
|
71 |
-
|
72 |
-
# Reload votes from file every 30 seconds
|
73 |
-
if 'last_updated' not in st.session_state:
|
74 |
-
st.session_state.last_updated = time.time()
|
75 |
-
|
76 |
-
if time.time() - st.session_state.last_updated > 30:
|
77 |
-
st.session_state.df = pd.read_csv('votes.csv')
|
78 |
-
st.session_state.last_updated = time.time()
|
|
|
2 |
import pandas as pd
|
3 |
import time
|
4 |
import matplotlib.pyplot as plt
|
5 |
+
import json
|
6 |
|
7 |
+
# Load USMLE questions from a JSON file
|
8 |
+
def load_questions():
|
9 |
+
return pd.DataFrame([{"question": "What is the serum level of X?", "answer": "High"},
|
10 |
+
{"question": "What causes Y?", "answer": "Z"}])
|
|
|
11 |
|
12 |
+
# Vote for a story
|
13 |
def vote_story(index):
|
14 |
st.session_state.df.loc[index, 'votes'] += 1
|
15 |
+
st.session_state.df.to_csv('votes.csv', index=False)
|
16 |
|
17 |
+
# Streamlit App
|
18 |
+
st.title("Medical App 🎙🗳️")
|
|
|
19 |
|
20 |
+
# Initialize session state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
if 'df' not in st.session_state:
|
22 |
try:
|
23 |
st.session_state.df = pd.read_csv('votes.csv')
|
24 |
except FileNotFoundError:
|
25 |
+
st.session_state.df = pd.DataFrame({'story': ['Story 1', 'Story 2'], 'votes': [0, 0]})
|
26 |
+
st.session_state.df.to_csv('votes.csv', index=False)
|
27 |
+
|
28 |
+
# Tab layout
|
29 |
+
tab = st.selectbox("Choose a Tab", ["USMLE Questions", "Story Voting"])
|
30 |
+
|
31 |
+
if tab == "USMLE Questions":
|
32 |
+
questions_df = load_questions()
|
33 |
+
selected_q = st.selectbox('Select a question:', questions_df['question'])
|
34 |
+
st.write(f"Answer: {questions_df.loc[questions_df['question'] == selected_q, 'answer'].values[0]}")
|
35 |
+
|
36 |
+
elif tab == "Story Voting":
|
37 |
+
for index, row in st.session_state.df.iterrows():
|
38 |
+
if st.button(f"Vote for: {row['story']}"):
|
39 |
+
vote_story(index)
|
40 |
+
st.table(st.session_state.df)
|
41 |
+
st.bar_chart(st.session_state.df.set_index('story')['votes'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|