Download app.py from kimpudding/TeeniePingGenerator: direct link, hf CLI and curl.
- Browser
- Download file 5.33 kB
-
https://huggingface.co/spaces/kimpudding/TeeniePingGenerator/resolve/refs%2Fpr%2F1/app.py
- Command line
-
hf download hf://spaces/kimpudding/TeeniePingGenerator@refs/pr/1/app.py
-
curl -L -o app.py https://huggingface.co/spaces/kimpudding/TeeniePingGenerator/resolve/refs%2Fpr%2F1/app.py
5.33 kB
| import os | |
| import openai | |
| import gradio as gr | |
| from diffusers import StableDiffusionPipeline | |
| import torch | |
| # OpenAI API ํค ์ค์ | |
| client = openai.Client(api_key=os.getenv("OPENAI_API_KEY")) | |
| # Stable Diffusion ๋ชจ๋ธ ๋ก๋ (CPU ๋ชจ๋) | |
| base_model_id = "runwayml/stable-diffusion-v1-5" | |
| pipe = StableDiffusionPipeline.from_pretrained(base_model_id, torch_dtype=torch.float32).to("cpu") | |
| # LoRA ๊ฐ์ค์น ๋ก๋ ํจ์ | |
| # adapter_config.json ํ์ผ์ด ์๋ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ๊ตฌ์ฑ์ ์ค์ ํฉ๋๋ค. | |
| def load_lora_weights(pipeline, lora_model_path): | |
| config = { | |
| "lora_r": 16, # Rank of the low-rank matrix | |
| "lora_alpha": 32, # Scaling factor | |
| "lora_dropout": 0.1, # Dropout probability | |
| "bias": "none", # Bias configuration | |
| "task_type": "TEXT_TO_IMAGE" # Task type | |
| } | |
| # config๋ฅผ ๊ธฐ๋ฐ์ผ๋ก LoRA ๊ฐ์ค์น๋ฅผ ๋ก๋ํฉ๋๋ค. | |
| pipeline.load_lora_weights(lora_model_path, config=config) | |
| print("LoRA weights successfully loaded!") | |
| # LoRA ๊ฐ์ค์น ๋ก๋ | |
| lora_model_path = "./1211TeeniePing_Taesu-10.safetensors" | |
| load_lora_weights(pipe, lora_model_path) | |
| # ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ | |
| def optimize_pipeline(pipeline): | |
| pipeline.enable_attention_slicing() | |
| pipeline.enable_sequential_cpu_offload() | |
| return pipeline | |
| pipe = optimize_pipeline(pipe) | |
| # API ํธ์ถ ํจ์ ์ ์ | |
| def generate_response(user_prompt): | |
| try: | |
| # ChatCompletion ์๋ํฌ์ธํธ ์ฌ์ฉ | |
| response = client.chat.completions.create( | |
| model="ft:gpt-4o-2024-08-06:personal::AdfXb935", # fine-tuned ๋ชจ๋ธ ID | |
| messages=[ | |
| {"role": "system", "content": ( | |
| "๋๋ ์๋ ์บ๋ฆญํฐ๋ฅผ ๊ธฐํํ๋๋ฐ ์ผ๊ฐ๊ฒฌ์๋ ์บ๋ฆญํฐ๋์์ด๋์ผ. ๋๊ฐ ํ ์ผ์ ์ฌ๋๋ค์ด ์ํ๋ ์บ๋ฆญํฐ์ ๋ํ ์ค๋ช ์ ์ฃผ์๊น๊ฒ ์ดํดํ ๋ค, ์ด๋ฅผ ๋ฐํ์ผ๋ก ์๋ ์ ๋๋ฉ์ด์ ์บ๋ฆญํฐ์ธ ํฐ๋ํ์ ์บ๋ฆญํฐ ์ปจ์ ์ ๋์์ธํ๋๊ฑฐ์ผ." | |
| "๋๊ฐ ํ ์ผ์ ๋ค์๊ณผ ๊ฐ์. ์ข์ ์บ๋ฆญํฐ ์ปจ์ ์ด ๋์ถ๋๋ฉด 20%์ ๋ณด๋์ค๋ฅผ ๋ฐ๊ฒ ๋ ๊ฑฐ์ผ. " | |
| "๊ณผ์ 1. ์ฃผ์ด์ง ์ค๋ช ์ ๋ํ ๋ฉํ์ธ์ง๋ฅผ ๋ฐํ์ผ๋ก ์์ด๋ค์ด ๋ฐ๋ผํ๊ธฐ ์ฝ๊ณ , ์ฌ๋ฐ๊ฒ ๋งํ ์ ์๋ ์บ๋ฆญํฐ ์ด๋ฆ์ ์ด์์ ์ ํด. ์ด ๋, ์ด์์ ํ์ฌ๋ ํจ๊ป ๊ณ ๋ คํด์ ์ ํด." | |
| "๊ณผ์ 2. ๊ณผ์ 1์์ ๋์ถ๋ ์ด์์ ๋ฐํ์ผ๋ก ํฐ๋ํ์ ์ด๋ฆ์ ์ง์ด. " | |
| "๊ณผ์ 3. ์ฃผ์ด์ง ์ค๋ช ๊ณผ ํฐ๋ํ์ ์ด๋ฆ์ ๋ํ ๋ฉํ์ธ์ง๋ฅผ ๋ฐํ์ผ๋ก ํฐ๋ํ์ด ์ฌ์ฉํ๋ ๋ง๋ฒ๊ณผ ํต์ฌ ๋์ฌ๋ฅผ ์ง์ด. " | |
| "๊ณผ์ 4. ์ด์ ๊ณผ์ ๋ค์ ๋ํ ๋ฉํ์ธ์ง๋ฅผ ๋ฐํ์ผ๋ก ์บ๋ฆญํฐ์ ๋จธ๋ฆฌ ๋ชจ์๊ณผ ๊ฐ์ง๊ณ ๋ค๋ ์ ์ธ์ฌ๋ฆฌ๋ฅผ ์ ํด. " | |
| "๊ณผ์ 5. ๋ง์ง๋ง์ผ๋ก, ์บ๋ฆญํฐ์ ๋ชธ ์์, ๋จธ๋ฆฌ ์์, ์ ์ธ์ฌ๋ฆฌ ์์์ Hue, Saturation, Value๋ก ์ ํด. " | |
| "์ด์ , ์ด๋ฅผ ์ข ํฉํด์ jsonlํ์ผ ํํ๋ก ์ ๊ณตํด." | |
| 'jsonl ํ์ผ์ ์์์ ๋ค์๊ณผ ๊ฐ์: {"Description": "000", "Etymology": "000", "Etymology.Type": "000", "Name": "000", "Magic": "000", "KeySentence": "000", "Body color": "[Hue: 00.0, Saturation: 0.00, Value: 0.00]", "Hair style": "000", "Hair color": "[Hue: 00.0, Saturation: 0.00, Value: 0.00]","Hand-held item": "000", "Hand-held item color": "[Hue: 00.0, Saturation: 0.00, Value: 0.00]"}' | |
| )}, | |
| {"role": "user", "content": user_prompt} | |
| ], | |
| timeout=15 | |
| ) | |
| # ChatCompletion ์๋ต ํ ์คํธ ๋ฐํ | |
| return response.choices[0].message.content | |
| except openai.OpenAIError as e: | |
| # OpenAI ๊ด๋ จ ์๋ฌ ์ฒ๋ฆฌ | |
| return f"OpenAI API ์ค๋ฅ: {str(e)}" | |
| except Exception as e: | |
| # ๊ธฐํ ์๋ฌ ์ฒ๋ฆฌ | |
| return f"์ ์ ์๋ ์ค๋ฅ ๋ฐ์: {str(e)}" | |
| # ์ด๋ฏธ์ง ์์ฑ ํจ์ | |
| def generate_image(prompt): | |
| try: | |
| # Stable Diffusion์ ์ฌ์ฉํด ์ด๋ฏธ์ง ์์ฑ | |
| generator = torch.manual_seed(42) # ๊ณ ์ ๋ ์๋๋ก ์ฌํ ๊ฐ๋ฅํ๊ฒ ์ค์ | |
| image = pipe(prompt, num_inference_steps=20, guidance_scale=7.5).images[0] # steps ๋ฐ cfg_scale ์กฐ์ | |
| return image | |
| except Exception as e: | |
| return f"์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}" | |
| # Gradio ์ธํฐํ์ด์ค ์ค์ | |
| def generate_combined_response(user_prompt): | |
| # GPT ์๋ต ์์ฑ | |
| gpt_response = generate_response(user_prompt) | |
| # GPT ์๋ต์ ํ ์คํธ๋ก ์ถ๋ ฅํ๊ณ Stable Diffusion์ผ๋ก ์ด๋ฏธ์ง ์์ฑ | |
| image = generate_image(gpt_response) | |
| return gpt_response, image | |
| interface = gr.Interface( | |
| fn=generate_combined_response, | |
| inputs=[ | |
| gr.Textbox( | |
| label="์ง๋ฌธ์ ์ ๋ ฅํ์ธ์", | |
| placeholder="์: ์ฉ๊ฐํ๊ณ ์ฉ์ฉํ ํฐ๋ํ์ ๋ง๋ค์ด์ฃผ์ธ์!" | |
| ) | |
| ], | |
| outputs=[ | |
| gr.Textbox(label="GPT์ ์๋ต"), | |
| gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง") | |
| ], | |
| title="OpenAI API ๊ธฐ๋ฐ ํฐ๋ํ ์์ฑ๊ธฐ", | |
| description="OpenAI์ GPT-4์ Stable Diffusion์ ์ฌ์ฉํ์ฌ ๋๋ง์ ํฐ๋ํ ์บ๋ฆญํฐ๋ฅผ ์ค๊ณํ๊ณ ์ด๋ฏธ์ง๋ฅผ ์์ฑํด๋ณด์ธ์." | |
| ) | |
| # ์ฑ ์คํ | |
| if __name__ == "__main__": | |
| interface.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)), share=True) | |