File size: 3,485 Bytes
9b7a547
e5df579
9b7a547
dd27f57
568361d
9b7a547
dd27f57
f5815bd
9b7a547
dd27f57
 
 
 
e5df579
9b7a547
 
 
e5df579
 
 
568361d
 
 
e5df579
 
 
dd27f57
9b7a547
 
568361d
 
dd27f57
9b7a547
 
 
e5df579
dd27f57
9b7a547
 
 
dd27f57
 
 
e5df579
dd27f57
9b7a547
 
 
 
 
 
 
e5df579
568361d
e5df579
568361d
e5df579
dd27f57
e5df579
b622977
 
e5df579
9b7a547
dd27f57
c1039cb
6303e29
6a90985
 
 
 
 
 
 
 
 
 
 
 
6303e29
6a90985
 
f5c7c60
dd27f57
 
e5df579
568361d
dd27f57
 
 
 
9b7a547
dd27f57
 
e5df579
dd27f57
 
6303e29
6a90985
 
 
 
 
 
 
 
 
 
 
 
6303e29
6a90985
7f17f6f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import os
import time
import requests
import gradio as gr
import uuid

# Hugging Face API
HF_API_KEY = os.getenv("HF_TOKEN")

# Chevereto API
CHEVERETO_API_URL = os.getenv("API_URL")
CHEVERETO_API_KEY = os.getenv("API_KEY")
CHEVERETO_ALBUM_ID = os.getenv("ALBUM_ID")
EXPECTED_API_KEY = os.getenv("API_PASS")

HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}

def generate_random_filename(extension: str) -> str:
    return str(uuid.uuid4()) + extension

def validate_api_key(api_key: str) -> bool:
    return api_key == EXPECTED_API_KEY

def generate_image(prompt: str, model_name: str):
    hf_api_url = f"https://api-inference.huggingface.co/models/{model_name}"
    response = requests.post(hf_api_url, headers=HEADERS, json={"inputs": prompt})
    
    if response.status_code == 200:
        image_data = response.content
        random_filename = generate_random_filename(".png")
        image_path = f"/tmp/{random_filename}"

        with open(image_path, "wb") as f:
            f.write(image_data)

        time.sleep(10)  # 10-sekundowa przerwa
        return image_path
    else:
        return f"Error generating image: {response.text}"

def upload_to_chevereto(image_path: str):
    with open(image_path, "rb") as img_file:
        files = {"source": img_file}
        data = {"key": CHEVERETO_API_KEY, "format": "json", "album_id": CHEVERETO_ALBUM_ID, "expiration": "PT10M"}
        
        response = requests.post(CHEVERETO_API_URL, files=files, data=data)
    
    if response.status_code == 200:
        return response.json().get("image", {}).get("url")
    else:
        return f"Error uploading image: {response.text}"

def generate_and_upload(prompt: str, model_name: str, api_key: str):
    if not validate_api_key(api_key):
        return "Invalid or missing API key", ""
    
    img_path = generate_image(prompt, model_name)
    if "Error" in img_path:
        return img_path, ""
    
    url = upload_to_chevereto(img_path)
    return img_path, url

# Gradio UI
with gr.Blocks(theme='NoCrypt/miku') as app:
    gr.HTML(
    """
    <style>
        .centered-title {
            text-align: center;
            color: red;
            font-size: 24px;
            font-weight: bold;
        }
    </style>
    <hr style='color: red;'>
    <h1 class='centered-title'>♥ Tasia API on Marty Space ♥</h1>
    <hr style='color: red;'>
    """
)
    
    gr.Markdown("<hr style='color: red;'>")
    with gr.Row():
        prompt_input = gr.Textbox(label="Enter a prompt:", placeholder="A futuristic spaceship...")
        model_input = gr.Textbox(label="Enter model name:", placeholder="stabilityai/stable-diffusion-3.5-large")
        api_key_input = gr.Textbox(label="Enter your API key:", type="password", placeholder="Your secret API key")
        generate_button = gr.Button("Generate & Upload")
    
    image_output = gr.Image(label="Generated Image")
    url_output = gr.Textbox(label="Image URL", interactive=False)

    generate_button.click(
        fn=generate_and_upload,
        inputs=[prompt_input, model_input, api_key_input],
        outputs=[image_output, url_output]
    )
   gr.HTML(
    """
    <style>
        .centered-title {
            text-align: center;
            color: red;
            font-size: 24px;
            font-weight: bold;
        }
    </style>
    <hr style='color: red;'>
    <h1 class='centered-title' ♥ Made by Tasia with Heart ♥</h1>
    <hr style='color: red;'>
    """
)
app.launch(server_port=7860)