|
import requests |
|
from flask import Flask, request, jsonify, send_file |
|
import logging |
|
import re |
|
import os |
|
import time |
|
import plotly.graph_objects as go |
|
import numpy as np |
|
from mpl_toolkits.mplot3d import Axes3D |
|
import matplotlib.pyplot as plt |
|
from io import BytesIO |
|
import matplotlib |
|
matplotlib.use('Agg') |
|
|
|
app = Flask(__name__) |
|
app.static_folder = 'static' |
|
logging.basicConfig(level=logging.DEBUG) |
|
|
|
@app.route('/v2/images/generations', methods=['POST']) |
|
def generate_image_cf(): |
|
|
|
data = request.json |
|
logging.debug(f"Received request data: {data}") |
|
|
|
|
|
cloudflare_account_id = data.get('CLOUDFLARE_ACCOUNT_ID') |
|
cloudflare_api_token = data.get('cloudflare_api_token') |
|
prompt = data.get('prompt') |
|
|
|
if not cloudflare_account_id or not cloudflare_api_token or not prompt: |
|
return jsonify({'error': 'Missing required parameters'}), 400 |
|
|
|
|
|
url = f"https://api.cloudflare.com/client/v4/accounts/{cloudflare_account_id}/ai/run/@cf/stabilityai/stable-diffusion-xl-base-1.0" |
|
headers = {"Authorization": f"Bearer {cloudflare_api_token}"} |
|
payload = {"prompt": prompt} |
|
|
|
|
|
response = requests.post(url, headers=headers, json=payload) |
|
logging.debug(f"Response status code: {response.status_code}") |
|
|
|
|
|
if response.status_code != 200: |
|
return jsonify({'error': 'Failed to generate image'}), response.status_code |
|
|
|
|
|
response_data = response.content |
|
logging.debug(f"Response content: {response_data}") |
|
|
|
|
|
img_filename = f'image_{int(time.time())}.png' |
|
save_path = os.path.join(app.static_folder, img_filename) |
|
|
|
|
|
with open(save_path, 'wb') as f: |
|
f.write(response_data) |
|
|
|
|
|
img_url = f'https://mistpe-flask.hf.space/static/{img_filename}' |
|
result = { |
|
"created": int(time.time()), |
|
"data": [ |
|
{"url": img_url} |
|
] |
|
} |
|
return jsonify(result) |
|
|
|
def preprocess_prompt(prompt): |
|
|
|
pattern = r'@startmindmap\n((?:.*?\n)*?)@endmindmap' |
|
match = re.search(pattern, prompt, re.DOTALL) |
|
if (match): |
|
mindmap_content = match.group(1) |
|
processed_prompt = f"@startmindmap\n{mindmap_content}\n@endmindmap" |
|
else: |
|
processed_prompt = prompt |
|
return processed_prompt |
|
|
|
@app.route('/v1/images/generations', methods=['POST']) |
|
def generate_image_plantuml(): |
|
|
|
data = request.json |
|
logging.debug(f"Received request data: {data}") |
|
|
|
|
|
processed_prompt = preprocess_prompt(data['prompt']) |
|
logging.debug(f"Processed prompt: {processed_prompt}") |
|
|
|
|
|
response = requests.post('https://mistpe-plantuml.hf.space/coder', data=processed_prompt.encode('utf-8')) |
|
logging.debug(f"Response status code: {response.status_code}") |
|
|
|
|
|
if response.status_code != 200: |
|
return jsonify({'error': 'Failed to generate image'}), response.status_code |
|
|
|
|
|
response_data = response.content |
|
logging.debug(f"Response content: {response_data}") |
|
|
|
created = 1589478378 |
|
url = f"https://mistpe-plantuml.hf.space/png/{response_data.decode('utf-8')}" |
|
|
|
|
|
result = { |
|
"created": created, |
|
"data": [ |
|
{ |
|
"url": url |
|
} |
|
] |
|
} |
|
return jsonify(result) |
|
|
|
@app.route('/api/3d-surface', methods=['POST']) |
|
def generate_3d_surface(): |
|
data = request.json |
|
prompt = data['prompt'] |
|
|
|
|
|
start_index = prompt.find("#start") + len("#start") |
|
end_index = prompt.find("#end") |
|
code = prompt[start_index:end_index].strip() |
|
|
|
|
|
local_vars = {} |
|
|
|
|
|
exec(code, globals(), local_vars) |
|
|
|
|
|
fig = local_vars.get('fig') |
|
|
|
|
|
if fig is None: |
|
return jsonify({"error": "No figure 'fig' defined in the provided code."}), 400 |
|
|
|
|
|
html_filename = f'3d_surface_plot_{int(time.time())}.html' |
|
fig.write_html(f'static/{html_filename}') |
|
|
|
|
|
html_url = f'https://mistpe-flask.hf.space/static/{html_filename}' |
|
|
|
|
|
img_filename = f'3d_surface_plot_{int(time.time())}.png' |
|
fig.write_image(f'static/{img_filename}') |
|
|
|
|
|
img_url = f'https://mistpe-flask.hf.space/static/{img_filename}' |
|
|
|
|
|
return jsonify({ |
|
"created": int(time.time()), |
|
"data": [ |
|
{"url": img_url}, |
|
{"url": html_url} |
|
] |
|
}) |
|
|
|
@app.route('/api/3d-sphere', methods=['POST']) |
|
def generate_3d_sphere(): |
|
|
|
code = request.json['prompt'] |
|
|
|
|
|
if code.startswith("```python") and code.endswith("```"): |
|
|
|
code = '\n'.join(code.split('\n')[1:-1]) |
|
|
|
|
|
fig = plt.figure() |
|
ax = fig.add_subplot(111, projection='3d') |
|
exec(code) |
|
|
|
|
|
buf = BytesIO() |
|
plt.savefig(buf, format='png') |
|
buf.seek(0) |
|
|
|
|
|
img_filename = f'3d_sphere_{int(time.time())}.png' |
|
save_path = os.path.join(app.static_folder, img_filename) |
|
|
|
|
|
with open(save_path, 'wb') as f: |
|
f.write(buf.getvalue()) |
|
|
|
|
|
img_url = f'https://mistpe-flask.hf.space/static/{img_filename}' |
|
return { |
|
"created": int(time.time()), |
|
"data": [ |
|
{ |
|
"url": img_url |
|
} |
|
] |
|
} |
|
|
|
@app.route('/', methods=['POST']) |
|
def health_check(): |
|
return jsonify({'status': 'OK'}), 200 |
|
|
|
if __name__ == '__main__': |
|
|
|
if not os.path.exists('static'): |
|
os.makedirs('static') |
|
app.run(host='0.0.0.0', port=7860, debug=True) |
|
|