File size: 1,513 Bytes
e2719e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests

def dowload_image(img_url):
    import random



    image_url = img_url
    # Define the local filename to save the image as
    i = random.choice(range(1,100))
    filename = f'downloaded_image{i}.png' 

    response = requests.get(image_url)

    # Check if the request was successful (status code 200)
    if response.status_code == 200:
        # Open the file in write-binary mode ('wb')
        with open(filename, 'wb') as file:
            file.write(response.content)
        print(f"Image downloaded successfully as {filename}!")
        return filename
    else:
        print(f"Failed to download image. Status code: {response.status_code}")



def text2Image(prompt):

    url = "https://ai-text-to-image-generator-flux-free-api.p.rapidapi.com/aaaaaaaaaaaaaaaaaiimagegenerator/quick.php"

    payload = {
        "prompt": prompt,
        "style_id": 4,
        "size": "1-1"
    }
    headers = {
        "x-rapidapi-key": "21a0339821mshb29d50e4919dc6dp1f25f6jsnbb6b8ece8c63",
        "x-rapidapi-host": "ai-text-to-image-generator-flux-free-api.p.rapidapi.com",
        "Content-Type": "application/json"
    }

    response = requests.post(url, json=payload, headers=headers)

    print(response.json())
    response = response.json()
    img_url =  response["result"]["data"]["results"][1]["origin"]

    return dowload_image(img_url=img_url)



gr.Interface(
    fn=text2Image,
    inputs=["text"],
    outputs=["image"],
    api_name="Testing.."
).launch()