Spaces:
Runtime error
Runtime error
Download app.py from unthinkablee/testtt: direct link, hf CLI and curl.
- Browser
- Download file 3.47 kB
-
https://huggingface.co/spaces/unthinkablee/testtt/resolve/main/app.py
- Command line
-
hf download hf://spaces/unthinkablee/testtt/app.py
-
curl -L -o app.py https://huggingface.co/spaces/unthinkablee/testtt/resolve/main/app.py
3.47 kB
| import os | |
| import openai | |
| import gradio as gr | |
| from PIL import Image | |
| import requests | |
| from io import BytesIO | |
| ################################### | |
| # 1) OpenAI API Key Ayarı | |
| ################################### | |
| openai.api_key = os.getenv("OPENAI_API_KEY", "sk-proj-_Oz1eXbKgsNPfaikhW_...") | |
| ################################### | |
| # 2) Eski DALL-E Edit Fonksiyonu | |
| ################################### | |
| def dall_e_edit(image_bytes, mask_bytes): | |
| """ | |
| openai==0.27.8 sürümünde Image.create_edit kullanarak | |
| 'Image Edit' çağrısı yapar. | |
| """ | |
| prompt_text = ( | |
| "Sadece beyaza boyadığım diş bölgesini gerçekçi dişlerle düzelt. " | |
| "Yüzün geri kalan kısımları olduğu gibi kalsın." | |
| ) | |
| try: | |
| response = openai.Image.create_edit( | |
| image=image_bytes, | |
| mask=mask_bytes, | |
| prompt=prompt_text, | |
| n=1, | |
| size="512x512", | |
| response_format="url" | |
| ) | |
| return response["data"][0]["url"] | |
| except Exception as e: | |
| print("DALL-E Edit Hatası:", e) | |
| return None | |
| ################################### | |
| # 3) Gradio'daki işlem fonksiyonu | |
| ################################### | |
| def process_image(sketch_data): | |
| """ | |
| Gradio 4.x 'tool="sketch"' parametresiyle input'tan | |
| (PIL_orijinal, PIL_maske) tuple gelir. | |
| """ | |
| if sketch_data is None: | |
| return "Lütfen bir resim yükleyin ve dişleri boyayın." | |
| # (orijinal PIL, maske PIL) | |
| if not (isinstance(sketch_data, tuple) and len(sketch_data) == 2): | |
| return "Veri formatı beklenmedik. Lütfen yeniden deneyin." | |
| orig_pil, mask_pil = sketch_data | |
| # 1) Orijinali bytes'a dönüştürelim | |
| orig_bytes = BytesIO() | |
| orig_pil.save(orig_bytes, format="PNG") | |
| orig_bytes.seek(0) | |
| # 2) Maske'yi bytes'a dönüştürelim | |
| mask_bytes = BytesIO() | |
| mask_pil.save(mask_bytes, format="PNG") | |
| mask_bytes.seek(0) | |
| # 3) DALL-E Edit çağıralım | |
| edited_url = dall_e_edit(orig_bytes, mask_bytes) | |
| if not edited_url: | |
| return "DALL-E isteği başarısız oldu." | |
| # 4) Sonuç resmi indirip PIL'e çevir | |
| resp = requests.get(edited_url) | |
| edited_pil = Image.open(BytesIO(resp.content)) | |
| return edited_pil | |
| ################################### | |
| # 4) Gradio Arayüz | |
| ################################### | |
| def build_app(): | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## Diş Ekleme (Gradio 4.44.1 + openai 0.27.8)") | |
| gr.Markdown( | |
| "1) Fotoğraf yükleyin\n" | |
| "2) Fırça ile diş bölgesini **beyaza** boyayın, geri kalan yer siyah kalsın\n" | |
| "3) **Diş Ekle** butonuna basın." | |
| ) | |
| # tool="sketch" => Tek output: (orijinal_PIL, maske_PIL) | |
| input_image = gr.Image( | |
| label="Orijinal Foto (Beyazla Dişleri Boyayın)", | |
| tool="sketch", | |
| type="pil", # Gradio result = (PIL, mask_PIL) | |
| source="upload" # Bilgisayardan yükle | |
| ) | |
| output_image = gr.Image(label="Sonuç", type="pil") | |
| btn = gr.Button("Diş Ekle") | |
| # Burada tek bir fonksiyon (process_image) yeterli. | |
| # input_image, "sketch" modunda, process_image'a tuple (orig, mask) gönderecek. | |
| btn.click( | |
| fn=process_image, | |
| inputs=input_image, | |
| outputs=output_image | |
| ) | |
| return demo | |
| if __name__ == "__main__": | |
| app = build_app() | |
| app.launch(server_name="0.0.0.0", server_port=7860) | |