Spaces:
Sleeping
Sleeping
freQuensy23
commited on
Commit
•
c0be431
1
Parent(s):
3bf54fe
INIT
Browse files- app.py +44 -142
- generators.py +106 -0
- requirements.txt +0 -0
- utils.py +25 -0
app.py
CHANGED
@@ -1,146 +1,48 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
import random
|
4 |
-
from diffusers import DiffusionPipeline
|
5 |
-
import torch
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
)
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
margin: 0 auto;
|
49 |
-
max-width: 520px;
|
50 |
-
}
|
51 |
-
"""
|
52 |
-
|
53 |
-
if torch.cuda.is_available():
|
54 |
-
power_device = "GPU"
|
55 |
-
else:
|
56 |
-
power_device = "CPU"
|
57 |
-
|
58 |
-
with gr.Blocks(css=css) as demo:
|
59 |
-
|
60 |
-
with gr.Column(elem_id="col-container"):
|
61 |
-
gr.Markdown(f"""
|
62 |
-
# Text-to-Image Gradio Template
|
63 |
-
Currently running on {power_device}.
|
64 |
-
""")
|
65 |
-
|
66 |
-
with gr.Row():
|
67 |
-
|
68 |
-
prompt = gr.Text(
|
69 |
-
label="Prompt",
|
70 |
-
show_label=False,
|
71 |
-
max_lines=1,
|
72 |
-
placeholder="Enter your prompt",
|
73 |
-
container=False,
|
74 |
-
)
|
75 |
-
|
76 |
-
run_button = gr.Button("Run", scale=0)
|
77 |
-
|
78 |
-
result = gr.Image(label="Result", show_label=False)
|
79 |
-
|
80 |
-
with gr.Accordion("Advanced Settings", open=False):
|
81 |
-
|
82 |
-
negative_prompt = gr.Text(
|
83 |
-
label="Negative prompt",
|
84 |
-
max_lines=1,
|
85 |
-
placeholder="Enter a negative prompt",
|
86 |
-
visible=False,
|
87 |
-
)
|
88 |
-
|
89 |
-
seed = gr.Slider(
|
90 |
-
label="Seed",
|
91 |
-
minimum=0,
|
92 |
-
maximum=MAX_SEED,
|
93 |
-
step=1,
|
94 |
-
value=0,
|
95 |
-
)
|
96 |
-
|
97 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
98 |
-
|
99 |
-
with gr.Row():
|
100 |
-
|
101 |
-
width = gr.Slider(
|
102 |
-
label="Width",
|
103 |
-
minimum=256,
|
104 |
-
maximum=MAX_IMAGE_SIZE,
|
105 |
-
step=32,
|
106 |
-
value=512,
|
107 |
-
)
|
108 |
-
|
109 |
-
height = gr.Slider(
|
110 |
-
label="Height",
|
111 |
-
minimum=256,
|
112 |
-
maximum=MAX_IMAGE_SIZE,
|
113 |
-
step=32,
|
114 |
-
value=512,
|
115 |
-
)
|
116 |
-
|
117 |
-
with gr.Row():
|
118 |
-
|
119 |
-
guidance_scale = gr.Slider(
|
120 |
-
label="Guidance scale",
|
121 |
-
minimum=0.0,
|
122 |
-
maximum=10.0,
|
123 |
-
step=0.1,
|
124 |
-
value=0.0,
|
125 |
-
)
|
126 |
-
|
127 |
-
num_inference_steps = gr.Slider(
|
128 |
-
label="Number of inference steps",
|
129 |
-
minimum=1,
|
130 |
-
maximum=12,
|
131 |
-
step=1,
|
132 |
-
value=2,
|
133 |
-
)
|
134 |
-
|
135 |
-
gr.Examples(
|
136 |
-
examples = examples,
|
137 |
-
inputs = [prompt]
|
138 |
-
)
|
139 |
-
|
140 |
-
run_button.click(
|
141 |
-
fn = infer,
|
142 |
-
inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
143 |
-
outputs = [result]
|
144 |
)
|
145 |
|
146 |
-
demo.
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
from generators import *
|
3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
from utils import async_zip_stream
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
|
10 |
+
async def handle(system_input: str, user_input: str):
|
11 |
+
print(system_input, user_input)
|
12 |
+
buffers = ["", "", "", ""]
|
13 |
+
async for outputs in async_zip_stream(
|
14 |
+
generate_gpt2(system_input, user_input),
|
15 |
+
generate_mistral_7bvo1(system_input, user_input),
|
16 |
+
generate_llama2(system_input, user_input),
|
17 |
+
generate_llama3(system_input, user_input),
|
18 |
+
):
|
19 |
+
# gpt_output, mistral_output, llama_output, llama2_output, llama3_output, llama4_output = outputs
|
20 |
+
for i, b in enumerate(buffers):
|
21 |
+
buffers[i] += str(outputs[i])
|
22 |
+
|
23 |
+
yield list(buffers) + ["", ""]
|
24 |
+
yield list(buffers) + [await generate_openllama(system_input, user_input),
|
25 |
+
await generate_bloom(system_input, user_input)]
|
26 |
+
|
27 |
+
|
28 |
+
with gr.Blocks() as demo:
|
29 |
+
system_input = gr.Textbox(label='System Input', value='You are AI assistant', lines=2)
|
30 |
+
with gr.Row():
|
31 |
+
gpt = gr.Textbox(label='gpt-2', lines=4, interactive=False)
|
32 |
+
mistral = gr.Textbox(label='mistral', lines=4, interactive=False)
|
33 |
+
llama = gr.Textbox(label='openllama', lines=4, interactive=False)
|
34 |
+
with gr.Row():
|
35 |
+
llama2 = gr.Textbox(label='llama-2', lines=4, interactive=False)
|
36 |
+
llama3 = gr.Textbox(label='llama-3', lines=4, interactive=False)
|
37 |
+
bloom = gr.Textbox(label='bloom', lines=4, interactive=False)
|
38 |
+
|
39 |
+
user_input = gr.Textbox(label='User Input', lines=2)
|
40 |
+
gen_button = gr.Button('Generate')
|
41 |
+
|
42 |
+
gen_button.click(
|
43 |
+
fn=handle,
|
44 |
+
inputs=[system_input, user_input],
|
45 |
+
outputs=[gpt, mistral, llama2, llama3, llama, bloom],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
)
|
47 |
|
48 |
+
demo.launch()
|
generators.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
|
5 |
+
import aiohttp
|
6 |
+
import gradio as gr
|
7 |
+
import numpy as np
|
8 |
+
import spaces
|
9 |
+
from huggingface_hub import InferenceClient
|
10 |
+
|
11 |
+
import random
|
12 |
+
import torch
|
13 |
+
from huggingface_hub import AsyncInferenceClient
|
14 |
+
from transformers import LlamaTokenizer, LlamaForCausalLM, AutoTokenizer
|
15 |
+
|
16 |
+
|
17 |
+
async def query_llm(payload, model_name):
|
18 |
+
headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
|
19 |
+
async with aiohttp.ClientSession() as session:
|
20 |
+
async with session.post(f"https://api-inference.huggingface.co/models/{model_name}", headers=headers,
|
21 |
+
json=payload) as response:
|
22 |
+
return await response.json()
|
23 |
+
|
24 |
+
|
25 |
+
async def generate_mistral_7bvo1(system_input, user_input):
|
26 |
+
client = AsyncInferenceClient(
|
27 |
+
"mistralai/Mistral-7B-Instruct-v0.1",
|
28 |
+
token=os.getenv('HF_TOKEN'),
|
29 |
+
)
|
30 |
+
|
31 |
+
async for message in await client.chat_completion(
|
32 |
+
messages=[
|
33 |
+
{"role": "system", "content": system_input},
|
34 |
+
{"role": "user", "content": user_input}, ],
|
35 |
+
max_tokens=256,
|
36 |
+
stream=True,
|
37 |
+
):
|
38 |
+
yield message.choices[0].delta.content
|
39 |
+
|
40 |
+
|
41 |
+
async def generate_gpt2(system_input, user_input):
|
42 |
+
output = await query_llm({
|
43 |
+
"inputs": (inputs:=f"{system_input}\n{user_input}"),
|
44 |
+
}, "openai-community/gpt2")
|
45 |
+
yield output[0]["generated_text"].replace(inputs, '')
|
46 |
+
|
47 |
+
|
48 |
+
async def generate_llama2(system_input, user_input):
|
49 |
+
client = AsyncInferenceClient(
|
50 |
+
"meta-llama/Llama-2-7b-chat-hf",
|
51 |
+
token=os.getenv('HF_TOKEN')
|
52 |
+
)
|
53 |
+
async for message in await client.chat_completion(
|
54 |
+
messages=[
|
55 |
+
{"role": "system", "content": system_input},
|
56 |
+
{"role": "user", "content": user_input}, ],
|
57 |
+
max_tokens=256,
|
58 |
+
stream=True,
|
59 |
+
):
|
60 |
+
yield message.choices[0].delta.content
|
61 |
+
|
62 |
+
|
63 |
+
@spaces.GPU
|
64 |
+
async def generate_openllama(system_input, user_input):
|
65 |
+
model_path = 'openlm-research/open_llama_3b_v2'
|
66 |
+
tokenizer = LlamaTokenizer.from_pretrained(model_path)
|
67 |
+
model = LlamaForCausalLM.from_pretrained(
|
68 |
+
model_path, torch_dtype=torch.float16, device_map='cuda',
|
69 |
+
)
|
70 |
+
# model = model.to("cuda")
|
71 |
+
input_text = f"{system_input}\n{user_input}"
|
72 |
+
input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to("cuda")
|
73 |
+
output = model.generate(input_ids, max_length=128)
|
74 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
75 |
+
|
76 |
+
|
77 |
+
@spaces.GPU
|
78 |
+
async def generate_bloom(system_input, user_input):
|
79 |
+
model_path = 'bigscience/bloom-7b1'
|
80 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
81 |
+
model = LlamaForCausalLM.from_pretrained(
|
82 |
+
model_path, torch_dtype=torch.float16, device_map='cuda',
|
83 |
+
)
|
84 |
+
input_text = f"{system_input}\n{user_input}"
|
85 |
+
input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to("cuda")
|
86 |
+
output = model.generate(input_ids, max_length=128)
|
87 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
async def generate_llama3(system_input, user_input):
|
92 |
+
client = AsyncInferenceClient(
|
93 |
+
"meta-llama/Meta-Llama-3.1-8B-Instruct",
|
94 |
+
token=os.getenv('HF_TOKEN')
|
95 |
+
)
|
96 |
+
try:
|
97 |
+
async for message in await client.chat_completion(
|
98 |
+
messages=[
|
99 |
+
{"role": "system", "content": system_input},
|
100 |
+
{"role": "user", "content": user_input}, ],
|
101 |
+
max_tokens=256,
|
102 |
+
stream=True,
|
103 |
+
):
|
104 |
+
yield message.choices[0].delta.content
|
105 |
+
except json.JSONDecodeError:
|
106 |
+
pass
|
requirements.txt
CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
|
|
utils.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
+
|
3 |
+
|
4 |
+
async def async_zip_stream(*iterators, default_value=''):
|
5 |
+
tasks = [asyncio.create_task(iterator.__anext__()) for iterator in iterators]
|
6 |
+
done = [False] * len(iterators)
|
7 |
+
|
8 |
+
while not all(done):
|
9 |
+
results = []
|
10 |
+
|
11 |
+
for i, task in enumerate(tasks):
|
12 |
+
if done[i]:
|
13 |
+
results.append(default_value)
|
14 |
+
elif task.done():
|
15 |
+
try:
|
16 |
+
results.append(task.result())
|
17 |
+
tasks[i] = asyncio.create_task(iterators[i].__anext__())
|
18 |
+
except StopAsyncIteration:
|
19 |
+
done[i] = True
|
20 |
+
results.append(default_value)
|
21 |
+
else:
|
22 |
+
results.append(default_value)
|
23 |
+
|
24 |
+
yield tuple(results)
|
25 |
+
await asyncio.sleep(0.01) # Slight delay to allow other tasks to progress
|