awacke1 commited on
Commit
86d2fb9
·
1 Parent(s): 2a58b4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -64
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
- # Function to persist and reload the dataframe
7
- def persist_and_reload(df):
8
- df.to_csv('votes.csv', index=False)
9
- new_df = pd.read_csv('votes.csv')
10
- return new_df
11
 
12
- # Function to vote on a story
13
  def vote_story(index):
14
  st.session_state.df.loc[index, 'votes'] += 1
15
- st.session_state.df = persist_and_reload(st.session_state.df)
16
 
17
- # Function to break story into sentences
18
- def break_into_sentences(story):
19
- return story.split(". ")
20
 
21
- # Function to extract high info words
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
- 'story': [
46
- "A 45-year-old man presents with a long history of ulcers on the bottom of his feet.",
47
- "A 24-year-old man, an information technology professional, gets himself tested for serum immunoglobulin M (IgM) levels.",
48
- "A 33-year-old woman who was recently involved in a motor vehicle accident presents to a medical clinic for a follow-up visit.",
49
- ],
50
- 'votes': [0, 0, 0]
51
- })
52
- st.session_state.df = persist_and_reload(st.session_state.df)
53
-
54
- # Main app
55
- st.title('Medical Story Voting 🗳️')
56
-
57
- # Display stories and voting buttons
58
- for index, row in st.session_state.df.iterrows():
59
- display_story(index, row)
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'])