Update myapp.py
Browse files
myapp.py
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
-
from flask import Flask, request, jsonify
|
2 |
from flask_cors import CORS
|
3 |
import os
|
4 |
from huggingface_hub import InferenceClient
|
5 |
from io import BytesIO
|
6 |
from PIL import Image
|
|
|
7 |
|
8 |
# Initialize the Flask app
|
9 |
myapp = Flask(__name__)
|
@@ -23,46 +24,30 @@ def generate_image(prompt, seed=1, model="prompthero/openjourney-v4"):
|
|
23 |
print(f"Error generating image: {str(e)}")
|
24 |
return None
|
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 |
-
return send_file(
|
51 |
-
img_byte_arr,
|
52 |
-
mimetype='image/png',
|
53 |
-
as_attachment=True, # Send the file as an attachment
|
54 |
-
download_name='generated_image.png' # The file name for download
|
55 |
-
)
|
56 |
-
else:
|
57 |
-
return jsonify({"error": "Failed to generate image"}), 500
|
58 |
-
except Exception as e:
|
59 |
-
print(f"Error in generate_api: {str(e)}") # Log the error
|
60 |
-
return jsonify({"error": str(e)}), 500
|
61 |
-
|
62 |
-
# Flask route for the UI
|
63 |
-
@myapp.route('/')
|
64 |
-
def index():
|
65 |
-
return render_template('index.html') # Serve the HTML page
|
66 |
|
67 |
# Add this block to make sure your app runs when called
|
68 |
if __name__ == "__main__":
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
from flask_cors import CORS
|
3 |
import os
|
4 |
from huggingface_hub import InferenceClient
|
5 |
from io import BytesIO
|
6 |
from PIL import Image
|
7 |
+
import gradio as gr # Import Gradio
|
8 |
|
9 |
# Initialize the Flask app
|
10 |
myapp = Flask(__name__)
|
|
|
24 |
print(f"Error generating image: {str(e)}")
|
25 |
return None
|
26 |
|
27 |
+
# Gradio interface function
|
28 |
+
def gradio_interface(prompt, seed, model_name):
|
29 |
+
image = generate_image(prompt, seed, model_name)
|
30 |
+
|
31 |
+
if image:
|
32 |
+
img_byte_arr = BytesIO()
|
33 |
+
image.save(img_byte_arr, format='PNG') # Convert the image to PNG
|
34 |
+
img_byte_arr.seek(0) # Move to the start of the byte stream
|
35 |
+
return img_byte_arr # Return the image as bytes
|
36 |
+
else:
|
37 |
+
return "Failed to generate image"
|
38 |
+
|
39 |
+
# Set up the Gradio interface
|
40 |
+
gr.Interface(
|
41 |
+
fn=gradio_interface,
|
42 |
+
inputs=[
|
43 |
+
gr.Textbox(label="Prompt", placeholder="Enter a text prompt", lines=2),
|
44 |
+
gr.Number(label="Seed", value=1, precision=0),
|
45 |
+
gr.Textbox(label="Model Name", value="prompthero/openjourney-v4", placeholder="Enter model name"),
|
46 |
+
],
|
47 |
+
outputs="image",
|
48 |
+
title="Image Generation with Hugging Face",
|
49 |
+
description="Enter a prompt, seed, and model name to generate an image."
|
50 |
+
).launch() # Launch the Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
# Add this block to make sure your app runs when called
|
53 |
if __name__ == "__main__":
|