Spaces:
Runtime error
Runtime error
create file
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import requests
|
4 |
+
import time
|
5 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
6 |
+
import paddlehub as hub
|
7 |
+
|
8 |
+
HF_TOKEN = os.environ["HF_TOKEN"]
|
9 |
+
model = hub.Module(name='ernie_vilg')
|
10 |
+
|
11 |
+
def get_ernie_vilg(text_prompts):
|
12 |
+
results = model.generate_image(text_prompts=text_prompts, style="油画", visualization=False)
|
13 |
+
return results[0]
|
14 |
+
|
15 |
+
sd_inf = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion", use_auth_token=HF_TOKEN)
|
16 |
+
|
17 |
+
nllb_model_name = 'facebook/nllb-200-distilled-600M'
|
18 |
+
nllb_model = AutoModelForSeq2SeqLM.from_pretrained(nllb_model_name)
|
19 |
+
nllb_tokenizer = AutoTokenizer.from_pretrained(nllb_model_name)
|
20 |
+
|
21 |
+
def get_chinese_translation(in_language_first, in_language_second, text):
|
22 |
+
print("********Inside get_chinese_translation ********")
|
23 |
+
print(f"text is :{text}, source language is : {in_language_first}, target language is : {in_language_second} ")
|
24 |
+
|
25 |
+
translator = pipeline('translation', model=nllb_model, tokenizer=nllb_tokenizer, src_lang=in_language_first, tgt_lang=in_language_second)
|
26 |
+
output = translator(text, max_length=400)
|
27 |
+
print(f"initial output is:{output}")
|
28 |
+
output = output[0]['translation_text']
|
29 |
+
print(f"output is:{output}")
|
30 |
+
|
31 |
+
return output
|
32 |
+
|
33 |
+
|
34 |
+
def get_sd(translated_txt, samples, steps, scale, seed):
|
35 |
+
print("******** Inside get_SD ********")
|
36 |
+
print(f"translated_txt is : {translated_txt}")
|
37 |
+
sd_img_gallery = sd_inf(translated_txt, samples, steps, scale, seed, fn_index=1)[0]
|
38 |
+
|
39 |
+
return sd_img_gallery
|
40 |
+
|
41 |
+
demo = gr.Blocks()
|
42 |
+
|
43 |
+
with demo:
|
44 |
+
gr.Markdown("Testing Diffusio models. STILL VERY MUCH WORK IN PROGRESS !!!!!!!!")
|
45 |
+
|
46 |
+
with gr.Row():
|
47 |
+
in_text_prompt = gr.Textbox(label="Enter English text here")
|
48 |
+
out_text_chinese = gr.Textbox(label="Your Chinese language output")
|
49 |
+
b1 = gr.Button("English to Chinese")
|
50 |
+
|
51 |
+
s1 = gr.Slider(label='samples', value=4, visible=False)
|
52 |
+
s2 = gr.Slider(label='steps', value=45, visible=False)
|
53 |
+
s3 = gr.Slider(label='scale', value=7.5, visible=False)
|
54 |
+
s4 = gr.Slider(label='seed', value=1024, visible=False)
|
55 |
+
|
56 |
+
in_language_first = gr.Dropdown(visible=False, value= 'eng_Latn') #'English')
|
57 |
+
in_language_second = gr.Dropdown(visible=False, value= 'zho_Hans') #'Chinese (Simplified)')
|
58 |
+
|
59 |
+
with gr.Row():
|
60 |
+
out_ernie = gr.Image(type="pil", label="Ernie output for the given prompt")
|
61 |
+
#out_gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery") #.style(grid=[2, 3], height="auto")
|
62 |
+
b2 = gr.Button("Generate Images from Ernie")
|
63 |
+
|
64 |
+
out_sd = gr.Image(type="pil", label="SD output for the given prompt")
|
65 |
+
b3 = gr.Button("Generate Images from SD")
|
66 |
+
|
67 |
+
b1.click(get_chinese_translation, [in_language_first, in_language_second, in_text_prompt], out_text_chinese )
|
68 |
+
b2.click(get_ernie_vilg, out_text_chinese, out_ernie) #out_gallery )
|
69 |
+
b3.click(get_sd, [in_text_prompt,s1,s2,s3,s4], out_sd) #out_gallery )
|
70 |
+
|
71 |
+
demo.launch(enable_queue=True, debug=True)
|