Update app.py
Browse files
app.py
CHANGED
@@ -1,104 +1,88 @@
|
|
1 |
-
import
|
2 |
-
from transformers import LLaMAForConditionalGeneration, LLaMATokenizer
|
3 |
import torch
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
from
|
8 |
-
from
|
9 |
-
import
|
10 |
-
import
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
tokenizer = LLaMATokenizer.from_pretrained("facebook/llama-3.1-base")
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
flow = InstalledAppFlow.from_client_secrets_file(
|
24 |
-
'credentials.json', SCOPES)
|
25 |
-
creds = flow.run_local_server(port=0)
|
26 |
-
drive_service = build('drive', 'v3', credentials=creds)
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
for page in range(pdf_file.numPages):
|
34 |
-
text += pdf_file.getPage(page).extractText()
|
35 |
-
return text
|
36 |
-
elif file.name.endswith('.csv') or file.name.endswith('.xlsx'):
|
37 |
-
if file.name.endswith('.csv'):
|
38 |
-
df = pd.read_csv(file)
|
39 |
-
else:
|
40 |
-
df = pd.read_excel(file)
|
41 |
-
return str(df)
|
42 |
-
elif file.name.endswith('.docx'):
|
43 |
-
# You need to implement a function to extract text from Word documents
|
44 |
-
# For simplicity, this example just returns an error message
|
45 |
-
return "Error: Word document support not implemented"
|
46 |
-
elif file.name.endswith('.gsheet'):
|
47 |
-
spreadsheet_id = file.name.split('/')[-1]
|
48 |
-
range_name = 'Sheet1!A1:Z1000' # You can change this range as needed
|
49 |
-
service = build('sheets', 'v4', credentials=creds)
|
50 |
-
sheet = service.spreadsheets()
|
51 |
-
result = sheet.values().get(spreadsheetId=spreadsheet_id,
|
52 |
-
range=range_name).execute()
|
53 |
-
values = result.get('values', [])
|
54 |
-
return str(values)
|
55 |
-
elif file.name.endswith('.gdoc'):
|
56 |
-
document_id = file.name.split('/')[-1]
|
57 |
-
service = build('docs', 'v1', credentials=creds)
|
58 |
-
doc = service.documents().get(documentId=document_id).execute()
|
59 |
-
text = ''
|
60 |
-
for element in doc.get('body').get('content'):
|
61 |
-
if 'paragraph' in element:
|
62 |
-
text += element.get('paragraph').get('elements')[0].get('textRun').get('content')
|
63 |
-
return text
|
64 |
-
elif file.name.endswith('.mp3'):
|
65 |
-
audio = pydub.AudioSegment.from_mp3(file)
|
66 |
-
text = ''
|
67 |
-
# You need to implement a function to transcribe audio
|
68 |
-
# For simplicity, this example just returns an error message
|
69 |
-
return "Error: Audio transcription support not implemented"
|
70 |
-
else:
|
71 |
-
return "Error: File type not supported"
|
72 |
|
73 |
-
#
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
title="LLaMA Chatbot",
|
86 |
-
description="Upload a file or paste some text, and ask a question about the content.",
|
87 |
-
)
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
content = process_file(file)
|
92 |
-
demo.update(inputs=[content])
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
outputs=None,
|
99 |
-
title="LLaMA Chatbot",
|
100 |
-
description="Upload a file to analyze its content.",
|
101 |
-
)
|
102 |
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
|
|
2 |
import torch
|
3 |
+
import random
|
4 |
+
import math
|
5 |
+
import time
|
6 |
+
from datetime import datetime
|
7 |
+
from typing import Union, List
|
8 |
+
from huggingface_hub import hf_hub_download
|
9 |
+
import numpy as np
|
10 |
+
import PIL.Image
|
11 |
+
from diffusers import CogVideoXPipeline, CogVideoXDDIMScheduler, CogVideoXDPMScheduler, VaeImageProcessor
|
12 |
+
from diffusers.utils import export_to_video
|
13 |
+
import moviepy.editor as mp
|
14 |
|
15 |
+
def download_file(repo_id, filename, subfolder):
|
16 |
+
return hf_hub_download(repo_id=repo_id, filename=filename, subfolder=subfolder)
|
|
|
17 |
|
18 |
+
def convert_to_gif(video_path):
|
19 |
+
clip = mp.VideoFileClip(video_path)
|
20 |
+
clip = clip.set_fps(8)
|
21 |
+
clip = clip.resize(height=240)
|
22 |
+
gif_path = video_path.replace(".mp4", ".gif")
|
23 |
+
clip.write_gif(gif_path, fps=8)
|
24 |
+
return gif_path
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
def save_video(tensor: Union[List[np.ndarray], List[PIL.Image.Image]], fps: int = 8):
|
27 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
28 |
+
video_path = f"{timestamp}.mp4"
|
29 |
+
export_to_video(tensor, video_path, fps=fps)
|
30 |
+
return video_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
# Downloading necessary files
|
33 |
+
scheduler_config_path = download_file("vdo/CogVideoX-5b", "scheduler_config.json", "scheduler")
|
34 |
+
text_encoder_config_path = download_file("vdo/CogVideoX-5b", "config.json", "text_encoder")
|
35 |
+
text_encoder_model_1_path = download_file("vdo/CogVideoX-5b", "model-00001-of-00002.safetensors", "text_encoder")
|
36 |
+
text_encoder_model_2_path = download_file("vdo/CogVideoX-5b", "model-00002-of-00002.safetensors", "text_encoder")
|
37 |
+
text_encoder_index_path = download_file("vdo/CogVideoX-5b", "model.safetensors.index.json", "text_encoder")
|
38 |
+
tokenizer_added_tokens_path = download_file("vdo/CogVideoX-5b", "added_tokens.json", "tokenizer")
|
39 |
+
tokenizer_special_tokens_map_path = download_file("vdo/CogVideoX-5b", "special_tokens_map.json", "tokenizer")
|
40 |
+
tokenizer_model_path = download_file("vdo/CogVideoX-5b", "spiece.model", "tokenizer")
|
41 |
+
tokenizer_config_path = download_file("vdo/CogVideoX-5b", "tokenizer_config.json", "tokenizer")
|
42 |
+
transformer_config_path = download_file("vdo/CogVideoX-5b", "config.json", "transformer")
|
43 |
+
transformer_model_1_path = download_file("vdo/CogVideoX-5b", "diffusion_pytorch_model-00001-of-00002.safetensors", "transformer")
|
44 |
+
transformer_model_2_path = download_file("vdo/CogVideoX-5b", "diffusion_pytorch_model-00002-of-00002.safetensors", "transformer")
|
45 |
+
transformer_index_path = download_file("vdo/CogVideoX-5b", "diffusion_pytorch_model.safetensors.index.json", "transformer")
|
46 |
+
vae_config_path = download_file("vdo/CogVideoX-5b", "config.json", "vae")
|
47 |
+
vae_model_path = download_file("vdo/CogVideoX-5b", "diffusion_pytorch_model.safetensors", "vae")
|
48 |
+
configuration_path = download_file("vdo/CogVideoX-5b", "configuration.json", "")
|
49 |
+
model_index_path = download_file("vdo/CogVideoX-5b", "model_index.json", "")
|
50 |
|
51 |
+
pipe = CogVideoXPipeline.from_pretrained("/content/CogVideoX-5b", torch_dtype=torch.float16)
|
52 |
+
pipe.enable_model_cpu_offload()
|
53 |
+
pipe.enable_sequential_cpu_offload()
|
54 |
+
pipe.vae.enable_slicing()
|
55 |
+
pipe.vae.enable_tiling()
|
|
|
|
|
|
|
56 |
|
57 |
+
prompt = "A golden retriever, sporting sleek black sunglasses, with its lengthy fur flowing in the breeze, sprints playfully across a rooftop terrace, recently refreshed by a light rain. The scene unfolds from a distance, the dog's energetic bounds growing larger as it approaches the camera, its tail wagging with unrestrained joy, while droplets of water glisten on the concrete behind it. The overcast sky provides a dramatic backdrop, emphasizing the vibrant golden coat of the canine as it dashes towards the viewer."
|
58 |
+
seed = 0
|
|
|
|
|
59 |
|
60 |
+
if seed == 0:
|
61 |
+
random.seed(int(time.time()))
|
62 |
+
seed = random.randint(0, 18446744073709551615)
|
63 |
+
print(seed)
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
with torch.inference_mode():
|
66 |
+
video_pt = pipe(
|
67 |
+
prompt=prompt,
|
68 |
+
num_videos_per_prompt=1,
|
69 |
+
num_inference_steps=50,
|
70 |
+
num_frames=49,
|
71 |
+
use_dynamic_cfg=True,
|
72 |
+
output_type="pt",
|
73 |
+
guidance_scale=7.0,
|
74 |
+
generator=torch.Generator(device="cpu").manual_seed(seed),
|
75 |
+
).frames
|
76 |
+
|
77 |
+
batch_size = video_pt.shape[0]
|
78 |
+
batch_video_frames = []
|
79 |
+
for batch_idx in range(batch_size):
|
80 |
+
pt_image = video_pt[batch_idx]
|
81 |
+
pt_image = torch.stack([pt_image[i] for i in range(pt_image.shape[0])])
|
82 |
+
|
83 |
+
image_np = VaeImageProcessor.pt_to_numpy(pt_image)
|
84 |
+
image_pil = VaeImageProcessor.numpy_to_pil(image_np)
|
85 |
+
batch_video_frames.append(image_pil)
|
86 |
+
|
87 |
+
video_path = save_video(batch_video_frames[0], fps=math.ceil((len(batch_video_frames[0]) - 1) / 6))
|
88 |
+
gif_path = convert_to_gif(video_path)
|