burak commited on
Commit
5511ee4
Β·
verified Β·
1 Parent(s): 8301430

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -1
app.py CHANGED
@@ -4,11 +4,14 @@ from typing import List, Tuple, Optional
4
  import google.generativeai as genai
5
  import gradio as gr
6
 
 
7
  GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
8
 
 
9
  TITLE = """<h1 align="center">πŸ‹οΈ AI Personal Trainer Playground πŸ’ͺ</h1>"""
10
  SUBTITLE = """<h3 align="center">Upload your workout video and let the AI analyze your form πŸ–‡οΈ</h3>"""
11
 
 
12
  Prompt = """
13
  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:
14
 
@@ -19,14 +22,17 @@ Overall Performance: Give an overall assessment of the workout, highlighting str
19
  Remember to be encouraging and supportive in your feedback. Your goal is to help them improve and stay motivated. Thank you!
20
  """
21
 
 
22
  def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
23
  if not stop_sequences:
24
  return None
25
  return [sequence.strip() for sequence in stop_sequences.split(",")]
26
 
 
27
  def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
28
  return "", chatbot + [[text_prompt, None]]
29
 
 
30
  def bot(
31
  google_key: str,
32
  model_name: str,
@@ -39,15 +45,18 @@ def bot(
39
  text_prompt_component: str,
40
  chatbot: List[Tuple[str, str]]
41
  ):
 
42
  google_key = google_key if google_key else GOOGLE_API_KEY
43
  if not google_key:
44
  raise ValueError(
45
  "GOOGLE_API_KEY is not set. "
46
  "Please follow the instructions in the README to set it up.")
47
 
 
48
  user_input = chatbot[-1][0]
49
  combined_prompt = Prompt + "\n" + user_input
50
 
 
51
  genai.configure(api_key=google_key)
52
  generation_config = genai.types.GenerationConfig(
53
  temperature=temperature,
@@ -56,9 +65,11 @@ def bot(
56
  top_k=top_k,
57
  top_p=top_p)
58
 
 
59
  if video_prompt is not None:
60
  model = genai.GenerativeModel(model_name)
61
 
 
62
  video_file = genai.upload_file(path=video_prompt)
63
  while video_file.state.name == "PROCESSING":
64
  print('.', end='')
@@ -68,6 +79,7 @@ def bot(
68
  if video_file.state.name == "FAILED":
69
  raise ValueError(video_file.state.name)
70
 
 
71
  response = model.generate_content(
72
  contents=[video_file, combined_prompt],
73
  stream=True,
@@ -82,7 +94,7 @@ def bot(
82
  generation_config=generation_config)
83
  response.resolve()
84
 
85
- # streaming effect
86
  chatbot[-1][1] = ""
87
  for chunk in response:
88
  for i in range(0, len(chunk.text), 10):
@@ -91,6 +103,7 @@ def bot(
91
  time.sleep(0.01)
92
  yield chatbot
93
 
 
94
  google_key_component = gr.Textbox(
95
  label="GOOGLE API KEY",
96
  value="",
@@ -172,6 +185,7 @@ top_p_component = gr.Slider(
172
  "the next token (using temperature). "
173
  ))
174
 
 
175
  user_inputs = [text_prompt_component, chatbot_component]
176
 
177
  bot_inputs = [
@@ -187,6 +201,7 @@ bot_inputs = [
187
  chatbot_component
188
  ]
189
 
 
190
  with gr.Blocks() as demo:
191
  gr.HTML(TITLE)
192
  gr.HTML(SUBTITLE)
@@ -206,6 +221,7 @@ with gr.Blocks() as demo:
206
  top_k_component.render()
207
  top_p_component.render()
208
 
 
209
  run_button_component.click(
210
  fn=user,
211
  inputs=user_inputs,
@@ -224,6 +240,7 @@ with gr.Blocks() as demo:
224
  fn=bot, inputs=bot_inputs, outputs=[chatbot_component]
225
  )
226
 
 
227
  gr.Examples(
228
  fn=bot,
229
  inputs=bot_inputs,
@@ -281,4 +298,5 @@ with gr.Blocks() as demo:
281
  #cache_examples="lazy",
282
  )
283
 
 
284
  demo.queue(max_size=99).launch(debug=False, show_error=True)
 
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
 
 
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,
 
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,
 
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='')
 
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,
 
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):
 
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="",
 
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 = [
 
201
  chatbot_component
202
  ]
203
 
204
+ # Create the Gradio interface
205
  with gr.Blocks() as demo:
206
  gr.HTML(TITLE)
207
  gr.HTML(SUBTITLE)
 
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,
 
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,
 
298
  #cache_examples="lazy",
299
  )
300
 
301
+ # Launch the Gradio interface
302
  demo.queue(max_size=99).launch(debug=False, show_error=True)