Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,12 @@ from externalmod import gr_Interface_load
|
|
5 |
import asyncio
|
6 |
import os
|
7 |
from threading import RLock
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
lock = RLock()
|
10 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
@@ -55,6 +61,25 @@ def generate_api(model_str, prompt, seed=1):
|
|
55 |
return result # Path to generated image
|
56 |
return None
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
# Launch Gradio API without frontend
|
59 |
iface = gr.Interface(fn=generate_api, inputs=["text", "text", "number"], outputs="file")
|
60 |
iface.launch(show_api=True, share=True)
|
|
|
5 |
import asyncio
|
6 |
import os
|
7 |
from threading import RLock
|
8 |
+
from gradio_client import Client
|
9 |
+
from flask import Flask, request, jsonify, send_file
|
10 |
+
|
11 |
+
|
12 |
+
client = Client("Geek7/mdztxi2")
|
13 |
+
Client.predict(fn=async_infer_api)
|
14 |
|
15 |
lock = RLock()
|
16 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
61 |
return result # Path to generated image
|
62 |
return None
|
63 |
|
64 |
+
@app.route('/predict', methods=['POST'])
|
65 |
+
def async_infer_api():
|
66 |
+
data = request.get_json()
|
67 |
+
model_str = data['model_str']
|
68 |
+
prompt = data['prompt']
|
69 |
+
seed = data.get('seed', 1) # Default seed value to 1 if not provided
|
70 |
+
|
71 |
+
try:
|
72 |
+
# Run the asynchronous inference function
|
73 |
+
result_path = asyncio.run(async_infer(model_str, prompt, seed))
|
74 |
+
if result_path:
|
75 |
+
# Use send_file to return the generated image
|
76 |
+
return send_file(result_path, mimetype='image/png') # Adjust mimetype as needed
|
77 |
+
else:
|
78 |
+
return jsonify({"error": "Image generation failed."}), 500
|
79 |
+
except Exception as e:
|
80 |
+
return jsonify({"error": str(e)}), 500
|
81 |
+
|
82 |
+
|
83 |
# Launch Gradio API without frontend
|
84 |
iface = gr.Interface(fn=generate_api, inputs=["text", "text", "number"], outputs="file")
|
85 |
iface.launch(show_api=True, share=True)
|