IAMTFRMZA commited on
Commit
ec2d6c8
·
verified ·
1 Parent(s): 98d0a6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -95
app.py CHANGED
@@ -1,104 +1,88 @@
1
- import gradio as gr
2
- from transformers import LLaMAForConditionalGeneration, LLaMATokenizer
3
  import torch
4
- import pandas as pd
5
- from PyPDF2 import PdfFileReader
6
- from googleapiclient.discovery import build
7
- from google_auth_oauthlib.flow import InstalledAppFlow
8
- from google.auth.transport.requests import Request
9
- import pickle
10
- import pydub
 
 
 
 
11
 
12
- # Set up LLaMA model and tokenizer
13
- model = LLaMAForConditionalGeneration.from_pretrained("facebook/llama-3.1-base")
14
- tokenizer = LLaMATokenizer.from_pretrained("facebook/llama-3.1-base")
15
 
16
- # Set up Google API credentials
17
- SCOPES = ['https://www.googleapis.com/auth/drive']
18
- creds = None
19
- if creds is None or not creds.valid:
20
- if creds and creds.expired and creds.refresh_token:
21
- creds.refresh(Request())
22
- else:
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
- # Define function to process uploaded files
29
- def process_file(file):
30
- if file.name.endswith('.pdf'):
31
- pdf_file = PdfFileReader(file)
32
- text = ''
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
- # Define function to answer questions about the uploaded content
74
- def answer_question(content, question):
75
- inputs = tokenizer.encode(question, return_tensors="pt")
76
- outputs = model.generate(inputs, max_length=100)
77
- answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
78
- return answer
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
- # Create Gradio interface
81
- demo = gr.Interface(
82
- fn=answer_question,
83
- inputs=["file", "text"],
84
- outputs="text",
85
- title="LLaMA Chatbot",
86
- description="Upload a file or paste some text, and ask a question about the content.",
87
- )
88
 
89
- # Define function to update the Gradio interface with the uploaded file's content
90
- def update_interface(file):
91
- content = process_file(file)
92
- demo.update(inputs=[content])
93
 
94
- # Create Gradio interface with file upload
95
- demo_with_upload = gr.Interface(
96
- fn=update_interface,
97
- inputs=["file"],
98
- outputs=None,
99
- title="LLaMA Chatbot",
100
- description="Upload a file to analyze its content.",
101
- )
102
 
103
- # Launch Gradio interface
104
- demo_with_upload.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)