Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import google.generativeai as genai
|
3 |
+
from pathlib import Path
|
4 |
+
import tempfile
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
def summarize_video(video_path):
|
9 |
+
if video_path is None:
|
10 |
+
return "Please upload a video file."
|
11 |
+
|
12 |
+
try:
|
13 |
+
# Create a temporary file to store the video
|
14 |
+
with tempfile.NamedTemporaryFile(suffix=Path(video_path.name).suffix, delete=False) as tmp_file:
|
15 |
+
tmp_file.write(video_path.read())
|
16 |
+
video_file_path = tmp_file.name
|
17 |
+
|
18 |
+
# Create the prompt
|
19 |
+
prompt = "Summarize this video"
|
20 |
+
|
21 |
+
# Set up the model
|
22 |
+
model = genai.GenerativeModel(model_name="models/gemini-1.5-pro", api_key=os.environ['GOOGLE_API_KEY'])
|
23 |
+
|
24 |
+
# Make the LLM request
|
25 |
+
print("Making LLM inference request...")
|
26 |
+
response = model.generate_content([prompt, video_file_path],
|
27 |
+
request_options={"timeout": 2000})
|
28 |
+
|
29 |
+
return response.text
|
30 |
+
|
31 |
+
except Exception as e:
|
32 |
+
return f"An error occurred: {str(e)}"
|
33 |
+
|
34 |
+
finally:
|
35 |
+
# Clean up temporary file
|
36 |
+
if 'video_file_path' in locals():
|
37 |
+
Path(video_file_path).unlink(missing_ok=True)
|
38 |
+
|
39 |
+
# Create Gradio interface
|
40 |
+
iface = gr.Interface(
|
41 |
+
fn=summarize_video,
|
42 |
+
inputs=gr.Video(label="Upload Video"),
|
43 |
+
outputs=gr.Textbox(label="Summary", lines=10),
|
44 |
+
title="Video Summarizer",
|
45 |
+
description="Upload a video to get an AI-generated summary using Gemini 1.5 Pro.",
|
46 |
+
examples=[],
|
47 |
+
cache_examples=False
|
48 |
+
)
|
49 |
+
|
50 |
+
# Launch the interface
|
51 |
+
if __name__ == "__main__":
|
52 |
+
iface.launch(share=True)
|