Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,29 +46,35 @@ def encode_video(video_path):
|
|
| 46 |
return [Image.fromarray(f.astype("uint8")) for f in frames]
|
| 47 |
|
| 48 |
@spaces.GPU(duration=60)
|
| 49 |
-
def generate(
|
| 50 |
|
| 51 |
-
print(
|
| 52 |
-
print(video)
|
| 53 |
-
print(audio)
|
| 54 |
print(instruction)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
inputs = [{"role": "user", "content": [img, instruction]}]
|
| 59 |
-
elif video is not None:
|
| 60 |
-
vid = encode_video(video)
|
| 61 |
-
inputs = [{"role": "user", "content": vid + [instruction]}]
|
| 62 |
-
elif audio is not None:
|
| 63 |
-
aud, _ = librosa.load(audio, sr=16000, mono=True)
|
| 64 |
-
inputs = [{"role": "user", "content": [aud, instruction]}]
|
| 65 |
-
else:
|
| 66 |
return "No input provided."
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
params = {
|
| 69 |
-
"msgs":
|
| 70 |
"tokenizer": tokenizer,
|
| 71 |
-
#"processor": processor,
|
| 72 |
"sampling": sampling,
|
| 73 |
"temperature": temperature,
|
| 74 |
"top_p": top_p,
|
|
@@ -78,7 +84,7 @@ def generate(image, video, audio, instruction=DEFAULT_INPUT, sampling=False, tem
|
|
| 78 |
}
|
| 79 |
|
| 80 |
output = repo.chat(**params)
|
| 81 |
-
|
| 82 |
print(output)
|
| 83 |
|
| 84 |
return output
|
|
@@ -89,9 +95,7 @@ def cloud():
|
|
| 89 |
# Initialize
|
| 90 |
with gr.Blocks(css=css) as main:
|
| 91 |
with gr.Column():
|
| 92 |
-
input = gr.
|
| 93 |
-
input_2 = gr.Video(label="Video")
|
| 94 |
-
input_3 = gr.Audio(label="Audio", type="filepath")
|
| 95 |
instruction = gr.Textbox(lines=1, value=DEFAULT_INPUT, label="Instruction")
|
| 96 |
sampling = gr.Checkbox(value=False, label="Sampling")
|
| 97 |
temperature = gr.Slider(minimum=0.01, maximum=1.99, step=0.01, value=0.7, label="Temperature")
|
|
@@ -101,11 +105,11 @@ with gr.Blocks(css=css) as main:
|
|
| 101 |
max_tokens = gr.Slider(minimum=1, maximum=4096, step=1, value=512, label="Max Tokens")
|
| 102 |
submit = gr.Button("▶")
|
| 103 |
maintain = gr.Button("☁️")
|
| 104 |
-
|
| 105 |
with gr.Column():
|
| 106 |
output = gr.Textbox(lines=1, value="", label="Output")
|
| 107 |
|
| 108 |
-
submit.click(fn=generate, inputs=[input,
|
| 109 |
maintain.click(cloud, inputs=[], outputs=[], queue=False)
|
| 110 |
|
| 111 |
main.launch(show_api=True)
|
|
|
|
| 46 |
return [Image.fromarray(f.astype("uint8")) for f in frames]
|
| 47 |
|
| 48 |
@spaces.GPU(duration=60)
|
| 49 |
+
def generate(input=[], instruction=DEFAULT_INPUT, sampling=False, temperature=0.7, top_p=0.8, top_k=100, repetition_penalty=1.05, max_tokens=512):
|
| 50 |
|
| 51 |
+
print(input)
|
|
|
|
|
|
|
| 52 |
print(instruction)
|
| 53 |
|
| 54 |
+
content = []
|
| 55 |
+
if not files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
return "No input provided."
|
| 57 |
|
| 58 |
+
for file_path in files:
|
| 59 |
+
ext = os.path.splitext(file_path)[1].lower()
|
| 60 |
+
if ext in [".jpg", ".jpeg", ".png", ".bmp", ".gif"]:
|
| 61 |
+
img = Image.open(file_path).convert("RGB")
|
| 62 |
+
content.append(img)
|
| 63 |
+
elif ext in [".mp4", ".mov", ".avi", ".mkv"]:
|
| 64 |
+
frames = encode_video(file_path)
|
| 65 |
+
content.extend(frames)
|
| 66 |
+
elif ext in [".wav", ".mp3", ".flac", ".aac"]:
|
| 67 |
+
aud, _ = librosa.load(file_path, sr=16000, mono=True)
|
| 68 |
+
content.append(aud)
|
| 69 |
+
else:
|
| 70 |
+
continue
|
| 71 |
+
|
| 72 |
+
content.append(instruction)
|
| 73 |
+
inputs_payload = [{"role": "user", "content": content}]
|
| 74 |
+
|
| 75 |
params = {
|
| 76 |
+
"msgs": inputs_payload,
|
| 77 |
"tokenizer": tokenizer,
|
|
|
|
| 78 |
"sampling": sampling,
|
| 79 |
"temperature": temperature,
|
| 80 |
"top_p": top_p,
|
|
|
|
| 84 |
}
|
| 85 |
|
| 86 |
output = repo.chat(**params)
|
| 87 |
+
|
| 88 |
print(output)
|
| 89 |
|
| 90 |
return output
|
|
|
|
| 95 |
# Initialize
|
| 96 |
with gr.Blocks(css=css) as main:
|
| 97 |
with gr.Column():
|
| 98 |
+
input = gr.File(label="Input", file_count="multiple", file_types=["image", "video", "audio"], type="filepath", allow_reordering=True)
|
|
|
|
|
|
|
| 99 |
instruction = gr.Textbox(lines=1, value=DEFAULT_INPUT, label="Instruction")
|
| 100 |
sampling = gr.Checkbox(value=False, label="Sampling")
|
| 101 |
temperature = gr.Slider(minimum=0.01, maximum=1.99, step=0.01, value=0.7, label="Temperature")
|
|
|
|
| 105 |
max_tokens = gr.Slider(minimum=1, maximum=4096, step=1, value=512, label="Max Tokens")
|
| 106 |
submit = gr.Button("▶")
|
| 107 |
maintain = gr.Button("☁️")
|
| 108 |
+
|
| 109 |
with gr.Column():
|
| 110 |
output = gr.Textbox(lines=1, value="", label="Output")
|
| 111 |
|
| 112 |
+
submit.click(fn=generate, inputs=[input, instruction, sampling, temperature, top_p, top_k, repetition_penalty, max_tokens], outputs=[output], queue=False)
|
| 113 |
maintain.click(cloud, inputs=[], outputs=[], queue=False)
|
| 114 |
|
| 115 |
main.launch(show_api=True)
|