ManishThota commited on
Commit
b012810
·
verified ·
1 Parent(s): 9399086

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -40
app.py CHANGED
@@ -6,14 +6,9 @@ from src.video_model import describe_video
6
  from src.utils import parse_string, parse_annotations
7
  import os
8
 
9
-
10
  # --- Function to construct the final query ---
11
  def process_video_and_questions(video, standing, hands, location, screen):
12
- # Extract the video name (filename)
13
  video_name = os.path.basename(video)
14
-
15
-
16
- # Construct the query with the video name included
17
  query = f"Answer the questions from the video\n"
18
  additional_info = []
19
  if standing:
@@ -35,53 +30,34 @@ def process_video_and_questions(video, standing, hands, location, screen):
35
  final_query = query + " " + " ".join(additional_info)
36
  final_prompt = final_query + " " + end_query
37
 
38
- # Assuming your describe_video function handles the video processing
39
  response = describe_video(video, final_prompt)
40
  final_response = f"<video_name>{video_name}</video_name>" + " \n" + response
41
 
42
  conditions = {
43
- 'standing': (standing, 'standing: 1', 'standing: None'),
44
- 'hands': (hands, 'hands.free: 1', 'hands.free: None'),
45
- 'location': (location, 'indoors: 1', 'indoors: None'),
46
- 'screen': (screen, 'screen.interaction_yes: 1', 'screen.interaction_yes: None')
47
  }
48
 
49
  for key, (condition, to_replace, replacement) in conditions.items():
50
  if not condition:
51
  final_response = final_response.replace(to_replace, replacement)
52
-
53
- # if standing == False:
54
- # final_response = final_response.replace('standing: 1', 'standing: None')
55
- # elif hands == False:
56
- # final_response = final_response.replace('hands.free: 1', 'hands.free: None')
57
- # elif location == False:
58
- # final_response = final_response.replace('indoors: 1', 'indoors: None')
59
- # elif screen == False:
60
- # final_response = final_response.replace('screen.interaction_yes: 1', 'screen.interaction_yes: None')
61
-
62
 
63
  return final_response
64
 
65
-
66
-
67
-
68
  def output_to_csv(final_response):
69
- # Parse the string to get the content
70
  parsed_content = parse_string(final_response, ["video_name", "annotation"])
71
-
72
- # Extract video name (assuming there's only one video name)
73
  video_name = parsed_content['video_name'][0] if parsed_content['video_name'] else None
74
-
75
- # Convert the list of annotations into a dictionary
76
  annotations_dict = parse_annotations(parsed_content['annotation']) if parsed_content['annotation'] else {}
77
 
78
- # Combine the video name and annotation dictionary into a single row
79
  df = pd.DataFrame([{'video_name': video_name, **annotations_dict}])
80
-
81
 
 
 
 
82
 
83
- return df
84
-
85
 
86
  # Examples for the interface
87
  examples = [
@@ -98,14 +74,11 @@ examples = [
98
  ["videos/2016-01-28_0700_US_KNBC_Channel_4_News_at_11PM_629.56-633.99_in_the_future.mp4", True, False, False, True]
99
  ]
100
 
101
- # Title, description, and article for the interface
102
  title = "GSoC Super Raid Annotator"
103
  description = "Annotate Videos"
104
  article = "<p style='text-align: center'><a href='https://github.com/OpenBMB/MiniCPM-V' target='_blank'>Model GitHub Repo</a> | <a href='https://huggingface.co/openbmb/MiniCPM-V-2_6' target='_blank'>Model Page</a></p>"
105
 
106
- custom_theme = gr.themes.Soft(
107
- primary_hue="red",
108
- secondary_hue="red")
109
 
110
  with gr.Blocks(theme=custom_theme) as demo:
111
  gr.Markdown(f"# {title}")
@@ -124,16 +97,14 @@ with gr.Blocks(theme=custom_theme) as demo:
124
 
125
  with gr.Column():
126
  response = gr.Textbox(label="Video Description", show_label=True, show_copy_button=True)
127
- csv_output = gr.DataFrame(label="CSV Output", headers=["video_name", "annotation"])
128
 
129
- # Event handling for the Submit button
130
  submit_btn.click(
131
  fn=process_video_and_questions,
132
  inputs=[video, standing, hands, location, screen],
133
  outputs=response
134
  )
135
 
136
- # Event handling for the Generate CSV button
137
  generate_csv_btn.click(
138
  fn=output_to_csv,
139
  inputs=response,
@@ -142,4 +113,4 @@ with gr.Blocks(theme=custom_theme) as demo:
142
 
143
  gr.Examples(examples=examples, inputs=[video, standing, hands, location, screen])
144
 
145
- demo.launch(debug=False)
 
6
  from src.utils import parse_string, parse_annotations
7
  import os
8
 
 
9
  # --- Function to construct the final query ---
10
  def process_video_and_questions(video, standing, hands, location, screen):
 
11
  video_name = os.path.basename(video)
 
 
 
12
  query = f"Answer the questions from the video\n"
13
  additional_info = []
14
  if standing:
 
30
  final_query = query + " " + " ".join(additional_info)
31
  final_prompt = final_query + " " + end_query
32
 
 
33
  response = describe_video(video, final_prompt)
34
  final_response = f"<video_name>{video_name}</video_name>" + " \n" + response
35
 
36
  conditions = {
37
+ 'standing': (standing, 'standing: 1', 'standing: None'),
38
+ 'hands': (hands, 'hands.free: 1', 'hands.free: None'),
39
+ 'location': (location, 'indoors: 1', 'indoors: None'),
40
+ 'screen': (screen, 'screen.interaction_yes: 1', 'screen.interaction_yes: None')
41
  }
42
 
43
  for key, (condition, to_replace, replacement) in conditions.items():
44
  if not condition:
45
  final_response = final_response.replace(to_replace, replacement)
 
 
 
 
 
 
 
 
 
 
46
 
47
  return final_response
48
 
 
 
 
49
  def output_to_csv(final_response):
 
50
  parsed_content = parse_string(final_response, ["video_name", "annotation"])
 
 
51
  video_name = parsed_content['video_name'][0] if parsed_content['video_name'] else None
 
 
52
  annotations_dict = parse_annotations(parsed_content['annotation']) if parsed_content['annotation'] else {}
53
 
 
54
  df = pd.DataFrame([{'video_name': video_name, **annotations_dict}])
 
55
 
56
+ # Save the DataFrame as a CSV file
57
+ csv_file_path = f"{video_name}_annotations.csv"
58
+ df.to_csv(csv_file_path, index=False)
59
 
60
+ return csv_file_path # Return the path to the CSV file for download
 
61
 
62
  # Examples for the interface
63
  examples = [
 
74
  ["videos/2016-01-28_0700_US_KNBC_Channel_4_News_at_11PM_629.56-633.99_in_the_future.mp4", True, False, False, True]
75
  ]
76
 
 
77
  title = "GSoC Super Raid Annotator"
78
  description = "Annotate Videos"
79
  article = "<p style='text-align: center'><a href='https://github.com/OpenBMB/MiniCPM-V' target='_blank'>Model GitHub Repo</a> | <a href='https://huggingface.co/openbmb/MiniCPM-V-2_6' target='_blank'>Model Page</a></p>"
80
 
81
+ custom_theme = gr.themes.Soft(primary_hue="red", secondary_hue="red")
 
 
82
 
83
  with gr.Blocks(theme=custom_theme) as demo:
84
  gr.Markdown(f"# {title}")
 
97
 
98
  with gr.Column():
99
  response = gr.Textbox(label="Video Description", show_label=True, show_copy_button=True)
100
+ csv_output = gr.File(label="Download CSV", interactive=False)
101
 
 
102
  submit_btn.click(
103
  fn=process_video_and_questions,
104
  inputs=[video, standing, hands, location, screen],
105
  outputs=response
106
  )
107
 
 
108
  generate_csv_btn.click(
109
  fn=output_to_csv,
110
  inputs=response,
 
113
 
114
  gr.Examples(examples=examples, inputs=[video, standing, hands, location, screen])
115
 
116
+ demo.launch(debug=False)