File size: 1,883 Bytes
6d35cbb
 
7712ec1
6d35cbb
 
 
 
7712ec1
 
6d35cbb
 
7712ec1
 
 
 
 
 
 
 
 
 
 
 
6d35cbb
 
7712ec1
6d35cbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from gradio_client import Client
import json

def get_caption(image_in):
    client = Client("https://vikhyatk-moondream1.hf.space/")
    result = client.predict(
        image_in,  # filepath in 'image' Image component
        "Describe the image",  # str in 'Question' Textbox component
        api_name="/answer_question"
    )

    # JSON 형태로 파싱
    try:
        result_json = json.loads(result)
        caption = result_json['choices'][0]['text']
    except json.JSONDecodeError:
        print("Error: Response is not valid JSON.")
        return ""
    except (KeyError, IndexError):
        print("Error: Invalid format in JSON response.")
        return ""

    return caption


def get_lcm(prompt):
    client = Client("https://latent-consistency-lcm-lora-for-sdxl.hf.space/")
    images = []
    for _ in range(4):  # 4개의 이미지 생성
        result = client.predict(
            prompt,  # str in 'parameter_5' Textbox component
            0.3,  # float (numeric value between 0.0 and 5) in 'Guidance' Slider component
            8,  # float (numeric value between 2 and 10) in 'Steps' Slider component
            0,  # float (numeric value between 0 and 12013012031030) in 'Seed' Slider component
            True,  # bool in 'Randomize' Checkbox component
            api_name="/predict"
        )
        images.append(result[0])
    return images

def infer(image_in):
    caption = get_caption(image_in)
    img_vars = get_lcm(caption)
    return caption, img_vars

gr.Interface(
    title="ArXivGPT Image",
    description=" Image2Image Variation - LCM SDXL & Moondream1 using for image generation",
    fn=infer,
    inputs=[
        gr.Image(type="filepath", label="Image input")
    ],
    outputs=[
        gr.Textbox(label="Caption"),
        gr.Gallery(label="LCM Image variations")
    ]
).queue(max_size=25).launch()