Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cohere
|
2 |
+
import gradio as gr
|
3 |
+
import requests
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
|
7 |
+
def generate_text(prompt,maxi,cohere_api_key):
|
8 |
+
co = cohere.Client(cohere_api_key)
|
9 |
+
|
10 |
+
response = co.generate(prompt=prompt,
|
11 |
+
temperature=0,
|
12 |
+
max_tokens=maxi)
|
13 |
+
return response[0]
|
14 |
+
|
15 |
+
def generate_image(prompt,gradio_api_key):
|
16 |
+
r = requests.post('https://clipdrop-api.co/text-to-image/v1',
|
17 |
+
files={
|
18 |
+
'prompt': (None, prompt, 'text/plain')
|
19 |
+
},
|
20 |
+
headers={'x-api-key': gradio_api_key}
|
21 |
+
)
|
22 |
+
|
23 |
+
if r.ok:
|
24 |
+
images = Image.open(io.BytesIO(r.content))
|
25 |
+
return images
|
26 |
+
else:
|
27 |
+
raise ValueError("Failed to generate image")
|
28 |
+
|
29 |
+
def text_and_image_generator(prompt,maxi,cohere_api_key,gradio_api_key):
|
30 |
+
text = generate_text(f"Generate a complete meaningful story within {maxi} words based on {prompt}. Complete every sentences don't create a half story.",maxi,cohere_api_key)
|
31 |
+
image = generate_image(text,gradio_api_key)
|
32 |
+
title = generate_text(f"title for the story {prompt} within 5 words",5,cohere_api_key)
|
33 |
+
|
34 |
+
return title,text,image
|
35 |
+
|
36 |
+
app = gr.Interface(
|
37 |
+
title="Story and Image Generator",
|
38 |
+
fn=text_and_image_generator,
|
39 |
+
inputs = [gr.inputs.Textbox(label="Enter your prompt to generate a story"),
|
40 |
+
gr.inputs.Slider(1,1000,label="Story length"),gr.inputs.Textbox(type="password",label="Cohere API key"),gr.inputs.Textbox(type="password",label="Gradio API key")],
|
41 |
+
outputs= [gr.outputs.Textbox(label="Story title"),gr.outputs.Textbox(label="Story"),gr.outputs.Image(type="pil",label="Image based on the Generated story")]
|
42 |
+
)
|
43 |
+
|
44 |
+
app.launch(share=True)
|