Abhisesh7 commited on
Commit
0b50181
·
verified ·
1 Parent(s): cf959f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -34
app.py CHANGED
@@ -1,34 +1,40 @@
1
- import gradio as gr # For building the web interface
2
- from PIL import Image # To handle image processing
3
- import numpy as np # To create dummy images
4
-
5
- # Function to create a dummy image for testing
6
- def create_dummy_image(text):
7
- # Create a blank white image
8
- img = np.ones((256, 256, 3), dtype=np.uint8) * 255 # White image
9
- return Image.fromarray(img)
10
-
11
- # Main function to generate comic panels from a user-provided story
12
- def generate_comic_panels(story_description):
13
- scenes = story_description.split(". ") # Split story into scenes using periods
14
- images = []
15
-
16
- for scene in scenes:
17
- if len(scene.strip()) > 0: # Skip empty sentences
18
- image = create_dummy_image(scene) # Create a dummy image for each scene
19
- images.append(image) # Append the image to the list
20
-
21
- return images # Return the list of images to be displayed as comic panels
22
-
23
- # Gradio interface setup
24
- demo = gr.Interface(
25
- fn=generate_comic_panels, # Function to call when user interacts
26
- inputs=gr.Textbox(lines=5, placeholder="Enter your short story description here..."), # Story input
27
- outputs=gr.Gallery(label="Generated Comic Panels"), # Display images in a gallery format
28
- title="GenArt Narrative",
29
- description="Enter a short story description, and we'll transform it into a comic strip!"
30
- )
31
-
32
- # Launch the app
33
- if __name__ == "__main__":
34
- demo.launch()
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import DallEProcessor, DallEModel
3
+ from PIL import Image
4
+ import requests
5
+ from flask import Flask, request, jsonify
6
+ import io
7
+
8
+ app = Flask(__name__)
9
+
10
+ # Initialize the DALL-E mini model and processor
11
+ model_name = "dalle-mini/dalle-mini"
12
+ processor = DallEProcessor.from_pretrained(model_name)
13
+ model = DallEModel.from_pretrained(model_name)
14
+
15
+ @app.route('/generate', methods=['POST'])
16
+ def generate_image():
17
+ data = request.json
18
+ prompt = data.get('prompt', '')
19
+
20
+ # Generate images from prompt
21
+ inputs = processor(text=[prompt], return_tensors="pt")
22
+ outputs = model(**inputs)
23
+
24
+ # Post-process the generated image
25
+ generated_image = outputs.logits.argmax(-1)[0]
26
+ image = Image.fromarray(generated_image)
27
+
28
+ # Save image to a BytesIO object
29
+ img_byte_arr = io.BytesIO()
30
+ image.save(img_byte_arr, format='PNG')
31
+ img_byte_arr = img_byte_arr.getvalue()
32
+
33
+ return jsonify({'image': img_byte_arr.hex()})
34
+
35
+ @app.route('/')
36
+ def home():
37
+ return "Welcome to GenArt Narrative!"
38
+
39
+ if __name__ == '__main__':
40
+ app.run(debug=True)