# 11. Video (Video) Quality Response LLM (Gemini)

## Video (Video) Quality Response LLM (Gemini) <a href="#video-llm-gemini" id="video-llm-gemini"></a>

**Main flow**

1. `File API` Upload video files using
2. `GenerateContent` Request a question about the video through the request.
3. Check the generated response.
4. Delete the uploaded Video file.

**important:**

* This tutorial `File API` For authentication and access `API keys` Use.
* Uploaded files `API key` Connected to the cloud project.

Different `Gemini API` Unlike, `API key` has `File API` It also gives access to the data uploaded, `API key` Particular attention should be paid to keeping Esau safe.

**Reference**

* [Gemini API (Cookbook) - Video](https://ai.google.dev/api/rest/v1/models/generateContent#media)

**API KEY issuance**

* [link ](https://makersuite.google.com/app/apikey?hl=ko)Get API KEY from.
* Environment variable the user's Google API key `GOOGLE_API_KEY` Set to.

`.env` Enter it as below in the file.

```
GOOGLE_API_KEY=< User's API KEY>
```

```
# LangSmith Tracking Settings
# !pip install langchain-teddynote
from langchain_teddynote import logging

# Enter a project name
logging.langsmith("CH04-Models")
```

```
Start tracking LangSmith. 
[Project name] 
CH04-Models
```

```
# LangSmith Tracking Settings
# !pip install langchain-teddynote
from langchain_teddynote import logging

# Enter a project name
logging.langsmith("CH04-Gemini-Video")
```

```
 Start tracking LangSmith. 
[Project name] 
CH04-Gemini-Video 
```

### Video upload <a href="#id-1" id="id-1"></a>

`Gemini API` accepts video file types directly.

**Limiting**

* `File API` It accommodates less than 2 GB of files and can store up to 20 GB of files per project.
* The file is kept for 2 days and in the API **Undownloadable** .

This example uses videos posted on the Teddy Note YouTube channel. (You can also proceed by replacing it with another video)

* [⁇ ⁇ #PDF I've done everything I can to preprocess.](https://youtu.be/O3qFWRObAXw)

```
# After downloading the file teddynote-sample-video.mp4 Save to file
!wget "https://www.dropbox.com/scl/fi/ugue14fyo010jgc7wuh4g/teddynote-sample-video.mp4?rlkey=wcsiktklt7jgoibsluft3m6z9&st=prv4p2uu&dl=1" -qO teddynote-sample-video.mp4
```

Enter the path of the video file below.

```
# Naming videos file
video_file_name = "teddynote-sample-video.mp4"
```

Next `File API` Upload video files using

```
import google.generativeai as genai

# Output file upload progress message
print(f"Uploading files...")

# Upload a file and return a file object
video_file = genai.upload_file(path=video_file_name)

# Upload completion message and files URI output of power
print(f"Upload complete: {video_file.uri}")
```

```
The file is being uploaded... 
Upload completed: https://generativelanguage.googleapis.com/v1beta/files/b1d0iktswld 
```

After uploading the file, `files.get` You can confirm that the API has successfully completed the file by calling.

`files.get` Files associated with the cloud project to which the API key belongs. It allows you to check files uploaded to the API.

```
import time

# Check video file processing status
while video_file.state.name == "PROCESSING":
    # Output a message waiting for processing to be completed
    print("Please wait a moment while the video upload and preprocessing are completed....")
    # 10 Super wait
    time.sleep(10)
    # Update video file status
    video_file = genai.get_file(video_file.name)

# Raise exception if processing fails
if video_file.state.name == "FAILED":
    raise ValueError(video_file.state.name)

# Output processing completion message
print(
    f"\nVideo processing is complete!\nYou can now start chatting: " + video_file.uri
)

```

```
 Please wait for the video upload and pretreatment to complete... 
Please wait for the video upload and pretreatment to complete... 
Please wait for the video upload and pretreatment to complete... 

Video processing is complete! 
Now you can start the conversation: https://generativelanguage.googleapis.com/v1beta/files/b1d0iktswld 
```

```
After the video is uploaded, generate_content You can use the function to request a question about Video.
```

```
# Prompt
prompt = "Can you give me a brief summary of this video?"

# The model Gemini 1.5 Flash set to
model = genai.GenerativeModel(model_name="models/gemini-1.5-flash")

# LLM request for reply
response = model.generate_content(
    [prompt, video_file], request_options={"timeout": 600}
)
# Output the results
print(response.text)
```

```
This video is a tutorial on how to parse PDF files. Describes how to use layout analysis when parsing PDF files in Langchain and how to use the uptage layout analysis algorithm. It also shows how to parse PDF files using llama parser and extract images, tables, text, etc.  
 Finally, it shows how to combine multiple PDF pages into one markdown file in Langchain.  

 Tutorials will help you learn how to parse PDF files and learn how to use Langchain more effectively.  
```

Below is an example of stream output. ( `stream=True` Add option)

```
# Generate prompt
prompt = "In this video Gencon Please tell me the time of the relevant mentioned part, Please tell me what you said."

# The model Gemini 1.5 Flash set to
model = genai.GenerativeModel(model_name="models/gemini-1.5-flash")

# LLM Request a stream response
response = model.generate_content(
    [prompt, video_file], request_options={"timeout": 600}, stream=True
)

# Generated content output
for chunk in response:
    print(chunk.text, end="", flush=True)
```

```
References to Gencon come from 0:27.  

"There is another event today. Jencon, Zencon Event Whether it's an event for three people for free. I'm going to quiz in the middle. I'm going to tell you anything in the middle. Oh, those who listen live will be able to get to anyone easily, just fill out the questionnaire. So those who have time on the 20th of September will come here and take it with me and it will help again."  
```

### Delete file <a href="#id-2" id="id-2"></a>

Files are automatically deleted after 2 days `files.delete()` You can delete it manually using

```
# delete file
genai.delete_file(video_file.name)

# Print deletion completion message
print(f"deleted video: {video_file.uri}")

```
