awacke1 commited on
Commit
a98d89c
Β·
verified Β·
1 Parent(s): 3e3e1d1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +235 -0
app.py ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import json
3
+
4
+ urls = [
5
+ "https://huggingface.co/spaces/awacke1/CB-GR-Chatbot-Blenderbot",
6
+ "https://huggingface.co/spaces/awacke1/TTS-STT-Blocks",
7
+ "https://huggingface.co/spaces/awacke1/Prompt-Refinery-Text-to-Image-Generation",
8
+ "https://huggingface.co/spaces/awacke1/Video-Summary",
9
+ "https://huggingface.co/spaces/awacke1/AI-MovieMaker-Comedy",
10
+ "https://huggingface.co/spaces/awacke1/ChatGPT-Memory-Chat-Story-Generator",
11
+ "https://huggingface.co/spaces/awacke1/CloneAnyVoice",
12
+ "https://huggingface.co/spaces/awacke1/ChatGPT-Streamlit-2",
13
+ "https://huggingface.co/spaces/awacke1/WikipediaUltimateAISearch",
14
+ "https://huggingface.co/spaces/awacke1/RLHF.Cognitive.Episodic.Semantic.Memory",
15
+ "https://huggingface.co/spaces/awacke1/Memory-Shared",
16
+ "https://huggingface.co/spaces/awacke1/VideoSwap",
17
+ "https://huggingface.co/spaces/awacke1/AI-Wikipedia-Search",
18
+ "https://huggingface.co/spaces/awacke1/AutoMLUsingStreamlit-Plotly",
19
+ "https://huggingface.co/spaces/awacke1/NLP-Lyric-Chorus-Image",
20
+ "https://huggingface.co/spaces/awacke1/OpenAssistant-Chatbot-FTW-Open-Source",
21
+ "https://huggingface.co/spaces/awacke1/ChatGPTStreamlit7",
22
+ "https://huggingface.co/spaces/awacke1/MultiPDF-QA-ChatGPT-Langchain",
23
+ "https://huggingface.co/spaces/awacke1/SOTA-Plan",
24
+ "https://huggingface.co/spaces/awacke1/AIandSmartTools",
25
+ "https://huggingface.co/spaces/awacke1/3DVirtualFood",
26
+ "https://huggingface.co/spaces/awacke1/Gradio-Gallery-Health-Medical-Icon-Sets",
27
+ "https://huggingface.co/spaces/awacke1/DatasetAnalyzer",
28
+ "https://huggingface.co/spaces/awacke1/PrompTart",
29
+ "https://huggingface.co/spaces/awacke1/sileod-deberta-v3-base-tasksource-nli",
30
+ "https://huggingface.co/spaces/awacke1/File-Memory-Operations-Human-Feedback-Gradio",
31
+ "https://huggingface.co/spaces/awacke1/Bloom.Big.Science.Continual.Generator",
32
+ "https://huggingface.co/spaces/awacke1/Ontology-Gradio",
33
+ "https://huggingface.co/spaces/awacke1/HTML5-Aframe-3dMap-Flight",
34
+ "https://huggingface.co/spaces/awacke1/Bloom.Generative.Writer",
35
+ "https://huggingface.co/spaces/awacke1/Voice-ChatGPT-Streamlit-12",
36
+ "https://huggingface.co/spaces/awacke1/HTML5-AR-VR",
37
+ "https://huggingface.co/spaces/awacke1/AnimationAI",
38
+ "https://huggingface.co/spaces/awacke1/GenerativeWordsandImages",
39
+ "https://huggingface.co/spaces/awacke1/AR-VR-IOT-Demo",
40
+ "https://huggingface.co/spaces/awacke1/ArtStyleFoodsandNutrition",
41
+ "https://huggingface.co/spaces/awacke1/CarePlanQnAWithContext",
42
+ "https://huggingface.co/spaces/awacke1/VideoSummaryYoutube3",
43
+ "https://huggingface.co/spaces/awacke1/AW-01ST-CSV-Dataset-Analyzer",
44
+ "https://huggingface.co/spaces/awacke1/Try.Playing.Learning.Sharing.On.This",
45
+ "https://huggingface.co/spaces/awacke1/google-flan-t5-base",
46
+ "https://huggingface.co/spaces/awacke1/PubMed-Parrot-Paraphraser-on-T5",
47
+ "https://huggingface.co/spaces/awacke1/Writing-Grammar-And-Paraphrase-w-Pegasus",
48
+ "https://huggingface.co/spaces/awacke1/runwayml-stable-diffusion-v1-5",
49
+ "https://huggingface.co/spaces/awacke1/DockerGoFlanT5",
50
+ "https://huggingface.co/spaces/awacke1/GradioContinualGenerator",
51
+ "https://huggingface.co/spaces/awacke1/StreamlitSuperPowerCheatSheet"
52
+ ]
53
+
54
+ # Extract the last part of each URL (after the last '/') to serve as the name of the button
55
+ url_names = [url.split('/')[-1] for url in urls]
56
+
57
+ # Associate each URL with a relevant emoji based on keywords in its name
58
+ emoji_mapping = {
59
+ "Chatbot": "πŸ€–",
60
+ "TTS": "πŸ—£οΈ",
61
+ "STT": "πŸ‘‚",
62
+ "Video": "πŸŽ₯",
63
+ "MovieMaker": "🍿",
64
+ "ChatGPT": "πŸ’¬",
65
+ "Voice": "πŸŽ™οΈ",
66
+ "Wikipedia": "πŸ“–",
67
+ "Memory": "🧠",
68
+ "AI": "🧠",
69
+ "OpenAssistant": "🀝",
70
+ "3D": "πŸ•ΆοΈ",
71
+ "AR": "πŸ‘“",
72
+ "VR": "πŸ•ΆοΈ",
73
+ "Animation": "πŸ–ŒοΈ",
74
+ "Dataset": "πŸ“Š",
75
+ "Gradio": "πŸ“»",
76
+ "HTML5": "🌐",
77
+ "Writing": "✍️",
78
+ "Grammar": "πŸ–‹οΈ",
79
+ "Paraphrase": "πŸ”„",
80
+ "Streamlit": "🌠"
81
+ }
82
+
83
+ # Map each URL name to its most relevant emoji
84
+ url_emojis = []
85
+ for name in url_names:
86
+ associated_emoji = "πŸ”—" # Default emoji
87
+ for keyword, emoji in emoji_mapping.items():
88
+ if keyword in name:
89
+ associated_emoji = emoji
90
+ break
91
+ url_emojis.append(associated_emoji)
92
+
93
+ #url_emojis[:5], url_names[:5] # Display the first 5 URL names with their associated emojis
94
+
95
+ import streamlit as st
96
+ import json
97
+ import webbrowser
98
+
99
+ # Function to load the history of clicks from the text file
100
+ def load_history():
101
+ try:
102
+ with open("click_history.txt", "r") as f:
103
+ return json.load(f)
104
+ except FileNotFoundError:
105
+ return {url: 0 for url in urls}
106
+
107
+ # Function to save the updated history of clicks to the text file
108
+ def save_history(history):
109
+ with open("click_history.txt", "w") as f:
110
+ json.dump(history, f)
111
+
112
+ # Load the history of clicks
113
+ history = load_history()
114
+
115
+ # Display the buttons for each URL
116
+ for url, name, emoji in zip(urls, url_names, url_emojis):
117
+ if st.button(f"{emoji} {name}"):
118
+ # Open the URL in a new browser tab using JavaScript
119
+ st.write('<script>window.open("'+url+'", "_blank");</script>', unsafe_allow_html=True)
120
+ # Update the history of clicks
121
+ history[url] += 1
122
+ save_history(history)
123
+ # Display the number of times the URL was opened below its corresponding button
124
+ st.write(f"Clicked: {history[url]} times")
125
+
126
+ import time
127
+ from bokeh.plotting import figure
128
+ from bokeh.models import ColumnDataSource
129
+
130
+ # ... [rest of the initial code remains unchanged] ...
131
+
132
+ # Streamlit app
133
+ def main():
134
+
135
+ # Session state to hold the value of AutoRepeat button across reruns
136
+ if "auto_repeat" not in st.session_state:
137
+ st.session_state.auto_repeat = "On"
138
+ if "current_index" not in st.session_state:
139
+ st.session_state.current_index = 0 # Use 0 as a default index
140
+
141
+ # Load the history of clicks
142
+ history = load_history()
143
+
144
+ # Display the buttons for each URL
145
+ for url, name, emoji in zip(urls, url_names, url_emojis):
146
+ #if st.button(f"{emoji} {name}"):
147
+ if st.button(f"{emoji} {name}", key=url): # using the URL as the unique key
148
+ # Open the URL in a new browser tab using JavaScript
149
+ st.write('<script>window.open("'+url+'", "_blank");</script>', unsafe_allow_html=True)
150
+ # Update the history of clicks
151
+ history[url] += 1
152
+ save_history(history)
153
+ # Display the number of times the URL was opened below its corresponding button
154
+ st.write(f"Clicked: {history[url]} times")
155
+
156
+ def get_url_emojis(url_names):
157
+ url_emojis = []
158
+ for name in url_names:
159
+ associated_emoji = "πŸ”—"
160
+ for keyword, emoji in emoji_mapping.items():
161
+ if keyword in name:
162
+ associated_emoji = emoji
163
+ break
164
+ url_emojis.append(associated_emoji)
165
+ return url_emojis
166
+
167
+ def load_history():
168
+ try:
169
+ with open("click_history.txt", "r") as f:
170
+ return json.load(f)
171
+ except FileNotFoundError:
172
+ return {url: 0 for url in urls}
173
+
174
+ def save_history(history):
175
+ with open("click_history.txt", "w") as f:
176
+ json.dump(history, f)
177
+
178
+ def main():
179
+ if "auto_repeat" not in st.session_state:
180
+ st.session_state.auto_repeat = "On"
181
+
182
+ history = load_history()
183
+
184
+ # Create list of dictionaries with all data
185
+ url_data = [{'url': url, 'name': name, 'emoji': emoji, 'clicks': history[url]}
186
+ for url, name, emoji in zip(urls, url_names, get_url_emojis(url_names))]
187
+
188
+ # Sort alphabetically on initial load if no clicks
189
+ if all(data['clicks'] == 0 for data in url_data):
190
+ url_data.sort(key=lambda x: x['name'].lower())
191
+ else:
192
+ # Sort by clicks (descending) and then alphabetically for ties
193
+ url_data.sort(key=lambda x: (-x['clicks'], x['name'].lower()))
194
+
195
+ # Display buttons in columns
196
+ num_cols = min(4, len(url_data))
197
+ cols = st.columns(num_cols)
198
+
199
+ for i, data in enumerate(url_data):
200
+ col = cols[i % num_cols]
201
+ with col:
202
+ if st.button(f"{data['emoji']} {data['name']}", key=f"btn_{data['url']}"):
203
+ st.write(f'<script>window.open("{data["url"]}", "_blank");</script>',
204
+ unsafe_allow_html=True)
205
+ history[data['url']] += 1
206
+ save_history(history)
207
+ st.experimental_rerun() # Rerun to update sorting
208
+ st.write(f"Clicked: {data['clicks']} times")
209
+
210
+ # Display graph for non-zero clicks
211
+ non_zero_data = [d for d in url_data if d['clicks'] > 0]
212
+ if non_zero_data:
213
+ source = ColumnDataSource(data=dict(
214
+ urls=[d['name'] for d in non_zero_data],
215
+ counts=[d['clicks'] for d in non_zero_data]
216
+ ))
217
+
218
+ p = figure(x_range=[d['name'] for d in non_zero_data],
219
+ height=350,
220
+ title="Click Counts per URL",
221
+ toolbar_location=None)
222
+ p.vbar(x='urls', top='counts', width=0.9, source=source)
223
+ p.xaxis.major_label_orientation = 1.2
224
+ st.bokeh_chart(p)
225
+
226
+ # Timer logic
227
+ if st.session_state.auto_repeat == "On":
228
+ timer_placeholder = st.empty()
229
+ for i in range(10, 0, -1):
230
+ timer_placeholder.text(f"Reloading in {i} seconds...")
231
+ time.sleep(1)
232
+ st.rerun()
233
+
234
+ if __name__ == "__main__":
235
+ main()