burak commited on
Commit
61a1c75
โ€ข
1 Parent(s): e1925db

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -302
app.py DELETED
@@ -1,302 +0,0 @@
1
- import os
2
- import time
3
- from typing import List, Tuple, Optional
4
- import google.generativeai as genai
5
- import gradio as gr
6
-
7
- # Get the Google API key from environment variables
8
- GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
9
-
10
- # Define the title and subtitle for the Gradio interface
11
- TITLE = """<h1 align="center">๐Ÿ‹๏ธ AI Personal Trainer Playground ๐Ÿ’ช</h1>"""
12
- SUBTITLE = """<h3 align="center">Upload your workout video and let the AI analyze your form ๐Ÿ–‡๏ธ</h3>"""
13
-
14
- # Define the prompt for the AI model
15
- Prompt = """
16
- You are the world's best fitness expert. Your goal is to analyze in detail how people perform their exercises and sports movements. Watch the provided video carefully and give them constructive feedback in at least 10 sentences. Focus on the following aspects:
17
-
18
- Form and Technique: Identify any issues with the form and technique of the exercises being performed. Provide specific suggestions for improvement.
19
- Repetitions and Sets: Count the number of repetitions and sets for each exercise. Ensure they match the intended workout plan.
20
- Pacing and Timing: Evaluate the pacing and timing of the exercises. Suggest any adjustments needed to optimize performance.
21
- Overall Performance: Give an overall assessment of the workout, highlighting strengths and areas for improvement.
22
- Remember to be encouraging and supportive in your feedback. Your goal is to help them improve and stay motivated. Thank you!
23
- """
24
-
25
- # Function to preprocess stop sequences
26
- def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
27
- if not stop_sequences:
28
- return None
29
- return [sequence.strip() for sequence in stop_sequences.split(",")]
30
-
31
- # Function to handle user input
32
- def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
33
- return "", chatbot + [[text_prompt, None]]
34
-
35
- # Function to handle bot response
36
- def bot(
37
- google_key: str,
38
- model_name: str,
39
- video_prompt,
40
- temperature: float,
41
- max_output_tokens: int,
42
- stop_sequences: str,
43
- top_k: int,
44
- top_p: float,
45
- text_prompt_component: str,
46
- chatbot: List[Tuple[str, str]]
47
- ):
48
- # Use the provided Google API key or the one from environment variables
49
- google_key = google_key if google_key else GOOGLE_API_KEY
50
- if not google_key:
51
- raise ValueError(
52
- "GOOGLE_API_KEY is not set. "
53
- "Please follow the instructions in the README to set it up.")
54
-
55
- # Combine the user input with the predefined prompt
56
- user_input = chatbot[-1][0]
57
- combined_prompt = Prompt + "\n" + user_input
58
-
59
- # Configure the generative AI model
60
- genai.configure(api_key=google_key)
61
- generation_config = genai.types.GenerationConfig(
62
- temperature=temperature,
63
- max_output_tokens=max_output_tokens,
64
- stop_sequences=preprocess_stop_sequences(stop_sequences=stop_sequences),
65
- top_k=top_k,
66
- top_p=top_p)
67
-
68
- # Handle video prompt if provided
69
- if video_prompt is not None:
70
- model = genai.GenerativeModel(model_name)
71
-
72
- # Upload the video file
73
- video_file = genai.upload_file(path=video_prompt)
74
- while video_file.state.name == "PROCESSING":
75
- print('.', end='')
76
- time.sleep(10)
77
- video_file = genai.get_file(video_file.name)
78
-
79
- if video_file.state.name == "FAILED":
80
- raise ValueError(video_file.state.name)
81
-
82
- # Generate content based on the video and prompt
83
- response = model.generate_content(
84
- contents=[video_file, combined_prompt],
85
- stream=True,
86
- generation_config=generation_config,
87
- request_options={"timeout": 600})
88
- response.resolve()
89
- else:
90
- model = genai.GenerativeModel(model_name)
91
- response = model.generate_content(
92
- combined_prompt,
93
- stream=True,
94
- generation_config=generation_config)
95
- response.resolve()
96
-
97
- # Streaming effect for chatbot response
98
- chatbot[-1][1] = ""
99
- for chunk in response:
100
- for i in range(0, len(chunk.text), 10):
101
- section = chunk.text[i:i + 10]
102
- chatbot[-1][1] += section
103
- time.sleep(0.01)
104
- yield chatbot
105
-
106
- # Define Gradio components
107
- google_key_component = gr.Textbox(
108
- label="GOOGLE API KEY",
109
- value="",
110
- type="password",
111
- placeholder="...",
112
- info="You have to provide your own GOOGLE_API_KEY for this app to function properly",
113
- visible=GOOGLE_API_KEY is None
114
- )
115
-
116
- video_prompt_component = gr.Video(label="Video", autoplay=True)
117
-
118
- model_selection = gr.Dropdown(["gemini-1.5-flash-latest", "gemini-1.5-pro-latest"], label="Select Gemini Model", value="gemini-1.5-pro-latest")
119
-
120
- chatbot_component = gr.Chatbot(
121
- label='Gemini',
122
- bubble_full_width=False,
123
- scale=3, height=500
124
- )
125
- text_prompt_component = gr.Textbox(
126
- placeholder="Hi there!",
127
- label="Ask me anything and press Enter"
128
- )
129
- run_button_component = gr.Button()
130
- temperature_component = gr.Slider(
131
- minimum=0,
132
- maximum=1.0,
133
- value=0.6,
134
- step=0.05,
135
- label="Temperature",
136
- info=(
137
- "Temperature controls the degree of randomness in token selection. Lower "
138
- "temperatures are good for prompts that expect a true or correct response, "
139
- "while higher temperatures can lead to more diverse or unexpected results. "
140
- ))
141
- max_output_tokens_component = gr.Slider(
142
- minimum=1,
143
- maximum=2048,
144
- value=1024,
145
- step=1,
146
- label="Token limit",
147
- info=(
148
- "Token limit determines the maximum amount of text output from one prompt. A "
149
- "token is approximately four characters. The default value is 2048."
150
- ))
151
- stop_sequences_component = gr.Textbox(
152
- label="Add stop sequence",
153
- value="",
154
- type="text",
155
- placeholder="STOP, END",
156
- info=(
157
- "A stop sequence is a series of characters (including spaces) that stops "
158
- "response generation if the model encounters it. The sequence is not included "
159
- "as part of the response. You can add up to five stop sequences."
160
- ))
161
- top_k_component = gr.Slider(
162
- minimum=1,
163
- maximum=40,
164
- value=32,
165
- step=1,
166
- label="Top-K",
167
- info=(
168
- "Top-k changes how the model selects tokens for output. A top-k of 1 means the "
169
- "selected token is the most probable among all tokens in the model's "
170
- "vocabulary (also called greedy decoding), while a top-k of 3 means that the "
171
- "next token is selected from among the 3 most probable tokens (using "
172
- "temperature)."
173
- ))
174
- top_p_component = gr.Slider(
175
- minimum=0,
176
- maximum=1,
177
- value=1,
178
- step=0.01,
179
- label="Top-P",
180
- info=(
181
- "Top-p changes how the model selects tokens for output. Tokens are selected "
182
- "from most probable to least until the sum of their probabilities equals the "
183
- "top-p value. For example, if tokens A, B, and C have a probability of .3, .2, "
184
- "and .1 and the top-p value is .5, then the model will select either A or B as "
185
- "the next token (using temperature). "
186
- ))
187
-
188
- # Define user and bot inputs
189
- user_inputs = [text_prompt_component, chatbot_component]
190
-
191
- bot_inputs = [
192
- google_key_component,
193
- model_selection,
194
- video_prompt_component,
195
- temperature_component,
196
- max_output_tokens_component,
197
- stop_sequences_component,
198
- top_k_component,
199
- top_p_component,
200
- text_prompt_component,
201
- chatbot_component
202
- ]
203
-
204
- # Create the Gradio interface
205
- with gr.Blocks() as demo:
206
- gr.HTML(TITLE)
207
- gr.HTML(SUBTITLE)
208
- with gr.Column():
209
- google_key_component.render()
210
- with gr.Row():
211
- video_prompt_component.render()
212
- chatbot_component.render()
213
- text_prompt_component.render()
214
- run_button_component.render()
215
- with gr.Accordion("Parameters", open=False):
216
- model_selection.render()
217
- temperature_component.render()
218
- max_output_tokens_component.render()
219
- stop_sequences_component.render()
220
- with gr.Accordion("Advanced", open=False):
221
- top_k_component.render()
222
- top_p_component.render()
223
-
224
- # Define the interaction between user input and bot response
225
- run_button_component.click(
226
- fn=user,
227
- inputs=user_inputs,
228
- outputs=[text_prompt_component, chatbot_component],
229
- queue=False
230
- ).then(
231
- fn=bot, inputs=bot_inputs, outputs=[chatbot_component]
232
- )
233
-
234
- text_prompt_component.submit(
235
- fn=user,
236
- inputs=user_inputs,
237
- outputs=[text_prompt_component, chatbot_component],
238
- queue=False
239
- ).then(
240
- fn=bot, inputs=bot_inputs, outputs=[chatbot_component]
241
- )
242
-
243
- # Define example inputs for the Gradio interface
244
- gr.Examples(
245
- fn=bot,
246
- inputs=bot_inputs,
247
- outputs=[chatbot_component],
248
- examples=[
249
- [
250
- "",
251
- "gemini-1.5-pro-latest",
252
- "./example1.mp4",
253
- .7,
254
- 1024,
255
- "",
256
- 32,
257
- 1,
258
- "Give me some tips to improve my deadlift.",
259
- [("", "")]
260
- ],
261
- [
262
- "",
263
- "gemini-1.5-pro-latest",
264
- "./example2.mp4",
265
- .7,
266
- 1024,
267
- "",
268
- 32,
269
- 1,
270
- "How is my form?",
271
- [("", "")]
272
- ],
273
- [
274
- "",
275
- "gemini-1.5-pro-latest",
276
- "./example3.mp4",
277
- .7,
278
- 1024,
279
- "",
280
- 32,
281
- 1,
282
- "What improvements can I make?",
283
- [("", "")]
284
- ],
285
- [
286
- "",
287
- "gemini-1.5-pro-latest",
288
- "./example4.mp4",
289
- .7,
290
- 1024,
291
- "",
292
- 32,
293
- 1,
294
- "I just started working out. I'm not sure I'm doing it right. Can you check?",
295
- [("", "")]
296
- ]
297
- ],
298
- #cache_examples="lazy",
299
- )
300
-
301
- # Launch the Gradio interface
302
- demo.queue(max_size=99).launch(debug=False, show_error=True)