Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import io | |
| import base64 | |
| from PIL import Image | |
| # KRITISCH: URL deiner API setzen | |
| #API_URL = "# Korrektur: Verwende die .hf.space-URL deines API-Spaces | |
| API_URL = "https://astridkraft-upscaling-api.hf.space/upscale" | |
| def upscale_via_api(input_image: Image.Image, scale_factor: str): | |
| """Sendet Bild an die API und gibt Ergebnis zurück.""" | |
| try: | |
| # Bild in Bytes umwandeln | |
| img_byte_arr = io.BytesIO() | |
| input_image.save(img_byte_arr, format='PNG') | |
| img_byte_arr = img_byte_arr.getvalue() | |
| # Anfrage an API senden | |
| files = {'file': ('image.png', img_byte_arr, 'image/png')} | |
| data = {'scale': scale_factor} | |
| response = requests.post(API_URL, files=files, data=data) | |
| response.raise_for_status() # Fehler werfen bei 4xx/5xx | |
| result = response.json() | |
| print("API JSON:", result) | |
| if result.get('success'): | |
| # Base64-Bild dekodieren | |
| base64_str = result['image_base64'].split(',')[1] | |
| image_data = base64.b64decode(base64_str) | |
| return Image.open(io.BytesIO(image_data)) | |
| else: | |
| raise Exception("API returned unsuccessful response") | |
| except Exception as e: | |
| # Fehlerbild erstellen | |
| error_img = Image.new('RGB', (512, 100), color='red') | |
| from PIL import ImageDraw, ImageFont | |
| draw = ImageDraw.Draw(error_img) | |
| try: | |
| font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20) | |
| except: | |
| font = ImageFont.load_default() | |
| draw.text((10, 40), f"API Error: {str(e)}", fill="white", font=font) | |
| return error_img | |
| # Gradio UI | |
| with gr.Blocks(title="🖼️ Upscaler UI", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# Bild-Upscaler (API-Backend)") | |
| gr.Markdown(f"Sendet Bilder an die API: `{API_URL}`") | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_image = gr.Image(type="pil", label="📤 Bild hochladen") | |
| scale_radio = gr.Radio( | |
| choices=["2x", "3x", "3.5x", "4x", "4.5x", "5x"], | |
| value="4x", | |
| label="🔍 Skalierungsfaktor" | |
| ) | |
| upscale_btn = gr.Button("🔄 Upscaling starten", variant="primary") | |
| with gr.Column(): | |
| output_image = gr.Image(type="pil", label="📥 Vergrößertes Ergebnis") | |
| upscale_btn.click( | |
| fn=upscale_via_api, | |
| inputs=[input_image, scale_radio], | |
| outputs=output_image | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860, share=False) | |