Spaces:
Sleeping
Sleeping
| import json | |
| import gradio as gr | |
| import requests | |
| import os | |
| def get_presigned_url(Video_input): | |
| file_name = os.path.basename(Video_input) | |
| print(file_name) | |
| response = requests.get( | |
| "https://lfipbncruqwwpzis5owg7danqm0ezcjr.lambda-url.us-west-1.on.aws", | |
| params={"filename": file_name} | |
| ) | |
| if response.status_code == 200: | |
| data = json.loads(response.text) | |
| presigned_url = data['presignedUrl'] | |
| # Use the presigned_url and object_key in your Gradio app | |
| return presigned_url | |
| else: | |
| print(f"Error: Status code {response.status_code}") | |
| return None | |
| def upload_to_s3(video_input, presigned_url): | |
| url = presigned_url['url'] | |
| fields = presigned_url['fields'] | |
| key = presigned_url['fields']['key'] | |
| #print(fields) | |
| if video_input is None or presigned_url is None: | |
| return "No file selected or presigned URL not available." | |
| try: | |
| with open(video_input, 'rb') as file: | |
| response = requests.post(url, data=fields, files={"file": file}) | |
| # response = requests.put(presigned_url, data=file, headers={'Content-Type': 'video/mp4'}) | |
| if response.status_code == 204: | |
| return "File uploaded successfully.", key | |
| else: | |
| return f"Upload failed. Status code: {response.status_code}" | |
| except Exception as e: | |
| return f"Error during upload: {str(e)}" | |
| # def process_video(video_url): | |
| # #... (process video from S3 URL)... | |
| def handle_upload(video_input): | |
| presigned_url = get_presigned_url(video_input) | |
| # print(presigned_url) | |
| return upload_to_s3(video_input, presigned_url) | |
| def perform_inference(key): | |
| if key: | |
| api_url = "https://xvgvbxenint7vknztncwcxz2640aunqp.lambda-url.us-west-1.on.aws" | |
| response = requests.post(api_url, json={"objectKey": key}) | |
| # print(response.json()) | |
| json_response = response.json() | |
| # print(json_response['choices'][0]['message']['content']) | |
| return f"Inference completed on file: {json_response['choices'][0]['message']['content']}" | |
| else: | |
| return "No file uploaded for inference." | |
| with gr.Blocks() as demo: | |
| gr.HTML("<h1>Video Inference Demo.</h1>") | |
| gr.HTML("<p>Please upload short video clips upto 50MB for better experience!</p>") | |
| #video_input = gr.File(label="Upload Video") | |
| video_input=gr.Video(label="Upload Video"), | |
| upload_status = gr.Textbox(label="Upload Status", interactive=False) | |
| s3_url = gr.Textbox(label="S3 URL", visible=False) | |
| inference_result = gr.Textbox(label="Inference Result", interactive=False) | |
| # Chain functions using .then() | |
| upload_button = gr.Button("Upload and Run Inference") | |
| upload_button.click( | |
| fn=handle_upload, | |
| inputs=video_input, | |
| outputs=[upload_status, s3_url] | |
| ).then( | |
| fn=perform_inference, | |
| inputs=s3_url, # Use output of previous function automatically | |
| outputs=inference_result | |
| ) | |
| demo.launch(share=True) |