Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
|
5 |
+
class HaikuGenerator:
|
6 |
+
def __init__(self):
|
7 |
+
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
8 |
+
self.text_client = InferenceClient(token=HUGGINGFACE_API_TOKEN, model="HuggingFaceH4/zephyr-7b-beta")
|
9 |
+
self.image_client = InferenceClient(token=HUGGINGFACE_API_TOKEN, model="stabilityai/stable-diffusion-xl-base-1.0")
|
10 |
+
|
11 |
+
def generate_haiku(self, prompt):
|
12 |
+
system_message = "You are a Haiku generator."
|
13 |
+
response = ""
|
14 |
+
|
15 |
+
messages = [{"role": "system", "content": system_message},
|
16 |
+
{"role": "user", "content": prompt}]
|
17 |
+
|
18 |
+
# Get the chat completion response
|
19 |
+
message = self.text_client.chat_completion(
|
20 |
+
messages,
|
21 |
+
max_tokens=30,
|
22 |
+
stream=False,
|
23 |
+
temperature=0.7,
|
24 |
+
top_p=0.95,
|
25 |
+
)
|
26 |
+
|
27 |
+
# Extract the generated content
|
28 |
+
response = message['choices'][0]['message']['content']
|
29 |
+
|
30 |
+
return response.strip()
|
31 |
+
|
32 |
+
def text_to_image(self, prompt, style):
|
33 |
+
# Modify the prompt based on the selected style
|
34 |
+
if style == "Japanese":
|
35 |
+
prompt += ", in Japanese art style"
|
36 |
+
elif style == "oil painting":
|
37 |
+
prompt += ", in the style of an oil painting"
|
38 |
+
|
39 |
+
image = self.image_client.text_to_image(prompt)
|
40 |
+
return image
|
41 |
+
|
42 |
+
def gradio_interface(self):
|
43 |
+
with gr.Blocks(theme='JohnSmith9982/small_and_pretty', css="style.css") as demo:
|
44 |
+
gr.HTML("""
|
45 |
+
<center><h1 style="color:#02C160">HaiKool - Haiku Poem and Image Generator</h1></center>""")
|
46 |
+
gr.HTML("""
|
47 |
+
<center><h6 style="color:#02C160">Generate a Haiku poem and an image based on it</h6></center>""")
|
48 |
+
|
49 |
+
with gr.Column(elem_id="col-container"):
|
50 |
+
haiku_output = gr.Textbox(label="Generated Haiku", interactive=False)
|
51 |
+
image_output = gr.Image()
|
52 |
+
|
53 |
+
with gr.Row(elem_id="col-container"):
|
54 |
+
with gr.Column():
|
55 |
+
prompt = gr.Textbox(show_label=False, placeholder="Enter a prompt for the Haiku")
|
56 |
+
with gr.Column():
|
57 |
+
style = gr.Dropdown(label="Select Image Style", choices=["default", "Japanese", "oil painting"], value="default")
|
58 |
+
with gr.Column():
|
59 |
+
generate_button = gr.Button("Generate Haiku and Image", elem_classes="button")
|
60 |
+
|
61 |
+
# Define the function that integrates both steps: Haiku generation and image creation
|
62 |
+
def generate_haiku_and_image(prompt, style):
|
63 |
+
haiku = self.generate_haiku(prompt)
|
64 |
+
image = self.text_to_image(haiku, style)
|
65 |
+
return haiku, image
|
66 |
+
|
67 |
+
generate_button.click(generate_haiku_and_image, inputs=[prompt, style], outputs=[haiku_output, image_output])
|
68 |
+
prompt.submit(generate_haiku_and_image, inputs=[prompt, style], outputs=[haiku_output, image_output])
|
69 |
+
|
70 |
+
demo.launch(debug=True)
|
71 |
+
|
72 |
+
if __name__ == "__main__":
|
73 |
+
haiku_generator = HaikuGenerator()
|
74 |
+
haiku_generator.gradio_interface()
|