kumahiyo commited on
Commit
3675354
1 Parent(s): f8075a2

add translator

Browse files
Files changed (2) hide show
  1. main.py +9 -28
  2. requirements.txt +2 -1
main.py CHANGED
@@ -9,12 +9,8 @@ from fastapi import FastAPI
9
  from fastapi.staticfiles import StaticFiles
10
  from pydantic import BaseModel
11
  from pydantic import Field
12
- from diffusers import DiffusionPipeline
13
- from transformers import (
14
- pipeline,
15
- MBart50TokenizerFast,
16
- MBartForConditionalGeneration,
17
- )
18
 
19
  app = FastAPI()
20
 
@@ -50,6 +46,10 @@ def draw(data: Data):
50
  text = re.sub('^#', '', data.string)
51
  text = re.sub('_seed[1-4]?', '', text)
52
 
 
 
 
 
53
  # prompt = '(('+text+')) (( photograph )), highly detailed, sharp focus, 8k, 4k, (( photorealism )), detailed, saturated, portrait, 50mm, F/2.8, 1m away, ( global illumination, studio light, volumetric light ), ((( multicolor lights )))'
54
  prompt = '(('+text+')) (( photograph )), highly detailed, sharp focus, 8k, 4k, (( photorealism )), detailed, saturated, portrait, 50mm, F/2.8, 1m away, ((( multicolor lights )))'
55
  n_prompt = 'text, blurry, art, painting, rendering, drawing, sketch, (( ugly )), (( duplicate )), ( morbid ), (( mutilated )), ( mutated ), ( deformed ), ( disfigured ), ( extra limbs ), ( malformed limbs ), ( missing arms ), ( missing legs ), ( extra arms ), ( extra legs ), ( fused fingers ), ( too many fingers ), long neck, low quality, worst quality'
@@ -57,34 +57,15 @@ def draw(data: Data):
57
  # https://huggingface.co/docs/hub/spaces-sdks-docker-first-demo
58
  # how to validation: https://qiita.com/bee2/items/75d9c0d7ba20e7a4a0e9
59
  # https://github.com/huggingface/diffusers
60
- # https://github.com/huggingface/diffusers/pull/1142
61
-
62
- # Add language detection pipeline
63
- language_detection_model_ckpt = "papluca/xlm-roberta-base-language-detection"
64
- language_detection_pipeline = pipeline("text-classification",
65
- model=language_detection_model_ckpt,
66
- device=device_dict[device])
67
-
68
- # Add model for language translation
69
- trans_tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50-many-to-one-mmt")
70
- trans_model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-many-to-one-mmt").to(device)
71
 
72
- model_id = 'CompVis/stable-diffusion-v1-4'
73
 
74
  #pipe = StableDiffusionPipeline.from_pretrained(model_id)
75
- pipe = DiffusionPipeline.from_pretrained(
76
- model_id,
77
- custom_pipeline="multilingual_stable_diffusion",
78
- detection_pipeline=language_detection_pipeline,
79
- translation_model=trans_model,
80
- translation_tokenizer=trans_tokenizer,
81
- revision='fp16',
82
- torch_dtype=torch.float16
83
- )
84
  pipe.enable_attention_slicing() # reduce gpu usage
85
  pipe = pipe.to(device)
86
 
87
- generator = torch.Generator("cuda").manual_seed(seed)
88
  images = pipe(prompt, negative_prompt=n_prompt, guidance_scale=7.5, generator=generator, num_images_per_prompt=1).images
89
 
90
  if 0 < int(seedno) < 5:
 
9
  from fastapi.staticfiles import StaticFiles
10
  from pydantic import BaseModel
11
  from pydantic import Field
12
+ from diffusers import StableDiffusionPipeline
13
+ from googletrans import Translator
 
 
 
 
14
 
15
  app = FastAPI()
16
 
 
46
  text = re.sub('^#', '', data.string)
47
  text = re.sub('_seed[1-4]?', '', text)
48
 
49
+ translator = Translator()
50
+ translation = translator.translate(text, dest="en")
51
+ text = translation.text
52
+
53
  # prompt = '(('+text+')) (( photograph )), highly detailed, sharp focus, 8k, 4k, (( photorealism )), detailed, saturated, portrait, 50mm, F/2.8, 1m away, ( global illumination, studio light, volumetric light ), ((( multicolor lights )))'
54
  prompt = '(('+text+')) (( photograph )), highly detailed, sharp focus, 8k, 4k, (( photorealism )), detailed, saturated, portrait, 50mm, F/2.8, 1m away, ((( multicolor lights )))'
55
  n_prompt = 'text, blurry, art, painting, rendering, drawing, sketch, (( ugly )), (( duplicate )), ( morbid ), (( mutilated )), ( mutated ), ( deformed ), ( disfigured ), ( extra limbs ), ( malformed limbs ), ( missing arms ), ( missing legs ), ( extra arms ), ( extra legs ), ( fused fingers ), ( too many fingers ), long neck, low quality, worst quality'
 
57
  # https://huggingface.co/docs/hub/spaces-sdks-docker-first-demo
58
  # how to validation: https://qiita.com/bee2/items/75d9c0d7ba20e7a4a0e9
59
  # https://github.com/huggingface/diffusers
 
 
 
 
 
 
 
 
 
 
 
60
 
61
+ model_id = 'stabilityai/stable-diffusion-2'
62
 
63
  #pipe = StableDiffusionPipeline.from_pretrained(model_id)
64
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, revision='fp16', torch_dtype=torch.float16)
 
 
 
 
 
 
 
 
65
  pipe.enable_attention_slicing() # reduce gpu usage
66
  pipe = pipe.to(device)
67
 
68
+ generator = torch.Generator(device).manual_seed(seed)
69
  images = pipe(prompt, negative_prompt=n_prompt, guidance_scale=7.5, generator=generator, num_images_per_prompt=1).images
70
 
71
  if 0 < int(seedno) < 5:
requirements.txt CHANGED
@@ -2,4 +2,5 @@ fastapi==0.74.*
2
  requests==2.27.*
3
  sentencepiece==0.1.*
4
  uvicorn[standard]==0.17.*
5
- diffusers[torch]==0.13.*
 
 
2
  requests==2.27.*
3
  sentencepiece==0.1.*
4
  uvicorn[standard]==0.17.*
5
+ diffusers[torch]==0.13.*
6
+ googletrans==4.0.0-rc1