Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
import base64
|
7 |
+
|
8 |
+
API_URL = "https://genv01.plutovid.com/get_image"
|
9 |
+
COUNT_URL = "https://genv01.plutovid.com/get_image_count"
|
10 |
+
|
11 |
+
def get_total_generated_count():
|
12 |
+
try:
|
13 |
+
response = requests.get(COUNT_URL)
|
14 |
+
response.raise_for_status()
|
15 |
+
return f"Total Images Generated So Far: {response.json().get('count', 0)}"
|
16 |
+
except requests.exceptions.RequestException:
|
17 |
+
return "Could not fetch generation count."
|
18 |
+
|
19 |
+
def generate_from_hosted_api(prompt, progress=gr.Progress(track_tqdm=True)):
|
20 |
+
if not prompt:
|
21 |
+
raise gr.Error("Prompt cannot be empty.")
|
22 |
+
|
23 |
+
images = []
|
24 |
+
|
25 |
+
try:
|
26 |
+
response = requests.get(API_URL, params={'message': prompt}, stream=True)
|
27 |
+
response.raise_for_status()
|
28 |
+
|
29 |
+
for line in response.iter_lines():
|
30 |
+
if line:
|
31 |
+
decoded_line = line.decode('utf-8')
|
32 |
+
if decoded_line.startswith('event: status'):
|
33 |
+
pass
|
34 |
+
elif decoded_line.startswith('data:'):
|
35 |
+
try:
|
36 |
+
data_json = json.loads(decoded_line[len('data:'):])
|
37 |
+
if 'message' in data_json:
|
38 |
+
progress(0.5, desc=data_json['message'])
|
39 |
+
elif 'images' in data_json:
|
40 |
+
progress(0.9, desc="Decoding images...")
|
41 |
+
for b64_image in data_json['images']:
|
42 |
+
img_data = base64.b64decode(b64_image)
|
43 |
+
img = Image.open(io.BytesIO(img_data))
|
44 |
+
images.append(img)
|
45 |
+
except json.JSONDecodeError:
|
46 |
+
pass
|
47 |
+
|
48 |
+
except requests.exceptions.RequestException as e:
|
49 |
+
raise gr.Error(f"Failed to connect to the generation service: {e}")
|
50 |
+
|
51 |
+
if not images:
|
52 |
+
return None, "Generation failed or no images were returned."
|
53 |
+
|
54 |
+
return images, "Generation complete!"
|
55 |
+
|
56 |
+
|
57 |
+
title = "PlutoGen V0.1"
|
58 |
+
description = """
|
59 |
+
# Welcome to PlutoGen V0.1
|
60 |
+
|
61 |
+
**Crafting Realistic and Non-Detectable AI Content**
|
62 |
+
|
63 |
+
This is the first model to be released open-source in the near future by **[PlutoLabs](https://huggingface.co/plutolabs)**, a non-profit from Russia dedicated to keeping pace with current technology. Our goal with PlutoGen is to push the boundaries of AI content generation, making it virtually indistinguishable from human-created content.
|
64 |
+
|
65 |
+
While this very first version may not seem like much, it's a step towards infinite possibilities. If you like this project, please consider leaving a like or following our organization for future releases and the upcoming open-source release of v0.1!
|
66 |
+
|
67 |
+
Enter a prompt below to see what PlutoGen can do.
|
68 |
+
"""
|
69 |
+
|
70 |
+
with gr.Blocks(theme='soft') as demo:
|
71 |
+
gr.Markdown(f"<h1 style='text-align: center;'>{title}</h1>")
|
72 |
+
gr.Markdown(description)
|
73 |
+
|
74 |
+
with gr.Row():
|
75 |
+
total_count_output = gr.Textbox(label="Generation Stats", interactive=False)
|
76 |
+
|
77 |
+
with gr.Row():
|
78 |
+
with gr.Column(scale=2):
|
79 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="e.g., a photorealistic portrait of a cat wearing a tiny hat")
|
80 |
+
submit_button = gr.Button("Generate Images", variant="primary")
|
81 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
82 |
+
with gr.Column(scale=3):
|
83 |
+
image_output = gr.Gallery(label="Generated Images", columns=2, height="auto")
|
84 |
+
|
85 |
+
submit_button.click(
|
86 |
+
fn=generate_from_hosted_api,
|
87 |
+
inputs=[prompt_input],
|
88 |
+
outputs=[image_output, status_output]
|
89 |
+
).then(
|
90 |
+
fn=get_total_generated_count,
|
91 |
+
outputs=total_count_output
|
92 |
+
)
|
93 |
+
|
94 |
+
demo.load(get_total_generated_count, None, total_count_output)
|
95 |
+
|
96 |
+
if __name__ == "__main__":
|
97 |
+
demo.launch()
|