Geek7 commited on
Commit
68e2543
1 Parent(s): b89af03

Update myapp.py

Browse files
Files changed (1) hide show
  1. myapp.py +26 -41
myapp.py CHANGED
@@ -1,9 +1,10 @@
1
- from flask import Flask, request, jsonify, send_file, render_template
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
- # Flask route for the API endpoint to generate an image based on a text prompt
27
- @myapp.route('/generate_image', methods=['POST'])
28
- def generate_api():
29
- data = request.get_json()
30
-
31
- # Extract required fields from the request
32
- prompt = data.get('prompt', '')
33
- seed = data.get('seed', 1)
34
- model_name = data.get('model', 'prompthero/openjourney-v4') # Default model
35
-
36
- if not prompt:
37
- return jsonify({"error": "Prompt is required"}), 400
38
-
39
- try:
40
- # Call the generate_image function with the custom model name
41
- image = generate_image(prompt, seed, model_name)
42
-
43
- if image:
44
- # Save the image to a BytesIO object
45
- img_byte_arr = BytesIO()
46
- image.save(img_byte_arr, format='PNG') # Convert the image to PNG
47
- img_byte_arr.seek(0) # Move to the start of the byte stream
48
-
49
- # Send the generated image as an attachment
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__":