abdullahmazhar3 commited on
Commit
2eb947d
·
1 Parent(s): c4c5766

refactor(text2video): enhance video generation with audio and filter options

Browse files
app/nlp.py CHANGED
@@ -115,19 +115,53 @@ def generate_video(audio, text, filter_option):
115
  except Exception as e:
116
  return None, f"Error: {str(e)}"
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  def serve():
119
  class VideoGeneratorServicer(text2video_pb2_grpc.VideoGeneratorServicer):
120
  def Generate(self, request, context):
 
121
  prompt = request.prompt.strip()
122
- if not prompt:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  return text2video_pb2.VideoResponse(
124
  video_path="",
125
- message="Prompt cannot be empty.",
126
  status_code=400
127
  )
128
 
129
  try:
130
- video_path, _ = generate_video(prompt)
131
  if video_path and os.path.exists(video_path):
132
  return text2video_pb2.VideoResponse(
133
  video_path=video_path,
@@ -147,7 +181,7 @@ def serve():
147
  status_code=500
148
  )
149
 
150
- server = grpc.server(futures.ThreadPoolExecutor(max_workers=4))
151
  text2video_pb2_grpc.add_VideoGeneratorServicer_to_server(VideoGeneratorServicer(), server)
152
  server.add_insecure_port('[::]:50051')
153
  server.start()
@@ -155,7 +189,7 @@ def serve():
155
  server.wait_for_termination()
156
 
157
  iface = gr.Interface(
158
- fn=generate_video,
159
  inputs=[
160
  gr.Audio(type="filepath", label="🎙 Upload or Record Audio (Optional)"),
161
  gr.Textbox(label="📝 Enter Text Prompt (Required if no audio)", placeholder="e.g., A knight fighting a dragon in the clouds"),
 
115
  except Exception as e:
116
  return None, f"Error: {str(e)}"
117
 
118
+ def call_grpc_server(audio, text, filter_option):
119
+ with grpc.insecure_channel('localhost:50051') as channel:
120
+ stub = text2video_pb2_grpc.VideoGeneratorStub(channel)
121
+
122
+ request = text2video_pb2.VideoRequest(
123
+ prompt=text.strip() if text else "",
124
+ audio_path=audio if audio else "",
125
+ filter_option=filter_option
126
+ )
127
+
128
+ response = stub.Generate(request)
129
+ if response.status_code == 200:
130
+ return response.video_path, f"<div>{response.message}</div>"
131
+ else:
132
+ return None, f"<div>{response.message}</div>"
133
+
134
  def serve():
135
  class VideoGeneratorServicer(text2video_pb2_grpc.VideoGeneratorServicer):
136
  def Generate(self, request, context):
137
+ # Extract and sanitize fields
138
  prompt = request.prompt.strip()
139
+ audio_path = request.audio_path.strip()
140
+ filter_option = request.filter_option.strip()
141
+ print("---- Incoming Request ----")
142
+ print("Prompt:", request.prompt)
143
+ print("Audio Path:", request.audio_path)
144
+ print("Filter Option:", request.filter_option)
145
+
146
+ # Validate filter option
147
+ valid_filters = {"None", "Grayscale", "Sepia"}
148
+ if filter_option not in valid_filters:
149
+ return text2video_pb2.VideoResponse(
150
+ video_path="",
151
+ message=f"Invalid filter_option '{filter_option}'. Must be one of: {', '.join(valid_filters)}.",
152
+ status_code=400
153
+ )
154
+
155
+ # Ensure at least one form of input
156
+ if not prompt and not audio_path:
157
  return text2video_pb2.VideoResponse(
158
  video_path="",
159
+ message="Either prompt or audio path must be provided.",
160
  status_code=400
161
  )
162
 
163
  try:
164
+ video_path, _ = generate_video(audio_path, prompt, filter_option)
165
  if video_path and os.path.exists(video_path):
166
  return text2video_pb2.VideoResponse(
167
  video_path=video_path,
 
181
  status_code=500
182
  )
183
 
184
+ server = grpc.server(futures.ThreadPoolExecutor(max_workers=8))
185
  text2video_pb2_grpc.add_VideoGeneratorServicer_to_server(VideoGeneratorServicer(), server)
186
  server.add_insecure_port('[::]:50051')
187
  server.start()
 
189
  server.wait_for_termination()
190
 
191
  iface = gr.Interface(
192
+ fn=call_grpc_server,
193
  inputs=[
194
  gr.Audio(type="filepath", label="🎙 Upload or Record Audio (Optional)"),
195
  gr.Textbox(label="📝 Enter Text Prompt (Required if no audio)", placeholder="e.g., A knight fighting a dragon in the clouds"),
app/text2video.proto CHANGED
@@ -2,19 +2,18 @@ syntax = "proto3";
2
 
3
  package text2video;
4
 
5
- // Request message
6
- message TextPrompt {
7
  string prompt = 1;
 
 
8
  }
9
 
10
- // Response message
11
  message VideoResponse {
12
  string video_path = 1;
13
  string message = 2;
14
  int32 status_code = 3;
15
  }
16
 
17
- // Service definition
18
  service VideoGenerator {
19
- rpc Generate (TextPrompt) returns (VideoResponse);
20
  }
 
2
 
3
  package text2video;
4
 
5
+ message VideoRequest {
 
6
  string prompt = 1;
7
+ string audio_path = 2;
8
+ string filter_option = 3;
9
  }
10
 
 
11
  message VideoResponse {
12
  string video_path = 1;
13
  string message = 2;
14
  int32 status_code = 3;
15
  }
16
 
 
17
  service VideoGenerator {
18
+ rpc Generate (VideoRequest) returns (VideoResponse);
19
  }
app/text2video_pb2.py CHANGED
@@ -24,17 +24,17 @@ _sym_db = _symbol_database.Default()
24
 
25
 
26
 
27
- DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x10text2video.proto\x12\ntext2video\"\x1c\n\nTextPrompt\x12\x0e\n\x06prompt\x18\x01 \x01(\t\"I\n\rVideoResponse\x12\x12\n\nvideo_path\x18\x01 \x01(\t\x12\x0f\n\x07message\x18\x02 \x01(\t\x12\x13\n\x0bstatus_code\x18\x03 \x01(\x05\x32O\n\x0eVideoGenerator\x12=\n\x08Generate\x12\x16.text2video.TextPrompt\x1a\x19.text2video.VideoResponseb\x06proto3')
28
 
29
  _globals = globals()
30
  _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
31
  _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'text2video_pb2', _globals)
32
  if not _descriptor._USE_C_DESCRIPTORS:
33
  DESCRIPTOR._loaded_options = None
34
- _globals['_TEXTPROMPT']._serialized_start=32
35
- _globals['_TEXTPROMPT']._serialized_end=60
36
- _globals['_VIDEORESPONSE']._serialized_start=62
37
- _globals['_VIDEORESPONSE']._serialized_end=135
38
- _globals['_VIDEOGENERATOR']._serialized_start=137
39
- _globals['_VIDEOGENERATOR']._serialized_end=216
40
  # @@protoc_insertion_point(module_scope)
 
24
 
25
 
26
 
27
+ DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x10text2video.proto\x12\ntext2video\"I\n\x0cVideoRequest\x12\x0e\n\x06prompt\x18\x01 \x01(\t\x12\x12\n\naudio_path\x18\x02 \x01(\t\x12\x15\n\rfilter_option\x18\x03 \x01(\t\"I\n\rVideoResponse\x12\x12\n\nvideo_path\x18\x01 \x01(\t\x12\x0f\n\x07message\x18\x02 \x01(\t\x12\x13\n\x0bstatus_code\x18\x03 \x01(\x05\x32Q\n\x0eVideoGenerator\x12?\n\x08Generate\x12\x18.text2video.VideoRequest\x1a\x19.text2video.VideoResponseb\x06proto3')
28
 
29
  _globals = globals()
30
  _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
31
  _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'text2video_pb2', _globals)
32
  if not _descriptor._USE_C_DESCRIPTORS:
33
  DESCRIPTOR._loaded_options = None
34
+ _globals['_VIDEOREQUEST']._serialized_start=32
35
+ _globals['_VIDEOREQUEST']._serialized_end=105
36
+ _globals['_VIDEORESPONSE']._serialized_start=107
37
+ _globals['_VIDEORESPONSE']._serialized_end=180
38
+ _globals['_VIDEOGENERATOR']._serialized_start=182
39
+ _globals['_VIDEOGENERATOR']._serialized_end=263
40
  # @@protoc_insertion_point(module_scope)
app/text2video_pb2_grpc.py CHANGED
@@ -26,8 +26,7 @@ if _version_not_supported:
26
 
27
 
28
  class VideoGeneratorStub(object):
29
- """Service definition
30
- """
31
 
32
  def __init__(self, channel):
33
  """Constructor.
@@ -37,14 +36,13 @@ class VideoGeneratorStub(object):
37
  """
38
  self.Generate = channel.unary_unary(
39
  '/text2video.VideoGenerator/Generate',
40
- request_serializer=text2video__pb2.TextPrompt.SerializeToString,
41
  response_deserializer=text2video__pb2.VideoResponse.FromString,
42
  _registered_method=True)
43
 
44
 
45
  class VideoGeneratorServicer(object):
46
- """Service definition
47
- """
48
 
49
  def Generate(self, request, context):
50
  """Missing associated documentation comment in .proto file."""
@@ -57,7 +55,7 @@ def add_VideoGeneratorServicer_to_server(servicer, server):
57
  rpc_method_handlers = {
58
  'Generate': grpc.unary_unary_rpc_method_handler(
59
  servicer.Generate,
60
- request_deserializer=text2video__pb2.TextPrompt.FromString,
61
  response_serializer=text2video__pb2.VideoResponse.SerializeToString,
62
  ),
63
  }
@@ -69,8 +67,7 @@ def add_VideoGeneratorServicer_to_server(servicer, server):
69
 
70
  # This class is part of an EXPERIMENTAL API.
71
  class VideoGenerator(object):
72
- """Service definition
73
- """
74
 
75
  @staticmethod
76
  def Generate(request,
@@ -87,7 +84,7 @@ class VideoGenerator(object):
87
  request,
88
  target,
89
  '/text2video.VideoGenerator/Generate',
90
- text2video__pb2.TextPrompt.SerializeToString,
91
  text2video__pb2.VideoResponse.FromString,
92
  options,
93
  channel_credentials,
 
26
 
27
 
28
  class VideoGeneratorStub(object):
29
+ """Missing associated documentation comment in .proto file."""
 
30
 
31
  def __init__(self, channel):
32
  """Constructor.
 
36
  """
37
  self.Generate = channel.unary_unary(
38
  '/text2video.VideoGenerator/Generate',
39
+ request_serializer=text2video__pb2.VideoRequest.SerializeToString,
40
  response_deserializer=text2video__pb2.VideoResponse.FromString,
41
  _registered_method=True)
42
 
43
 
44
  class VideoGeneratorServicer(object):
45
+ """Missing associated documentation comment in .proto file."""
 
46
 
47
  def Generate(self, request, context):
48
  """Missing associated documentation comment in .proto file."""
 
55
  rpc_method_handlers = {
56
  'Generate': grpc.unary_unary_rpc_method_handler(
57
  servicer.Generate,
58
+ request_deserializer=text2video__pb2.VideoRequest.FromString,
59
  response_serializer=text2video__pb2.VideoResponse.SerializeToString,
60
  ),
61
  }
 
67
 
68
  # This class is part of an EXPERIMENTAL API.
69
  class VideoGenerator(object):
70
+ """Missing associated documentation comment in .proto file."""
 
71
 
72
  @staticmethod
73
  def Generate(request,
 
84
  request,
85
  target,
86
  '/text2video.VideoGenerator/Generate',
87
+ text2video__pb2.VideoRequest.SerializeToString,
88
  text2video__pb2.VideoResponse.FromString,
89
  options,
90
  channel_credentials,