Spaces:
Runtime error
Runtime error
| import time | |
| import os | |
| import numpy as np | |
| from mpl_toolkits.mplot3d import Axes3D | |
| import matplotlib.pyplot as plt | |
| from flask import Flask, request, send_file, jsonify | |
| from io import BytesIO | |
| import matplotlib | |
| matplotlib.use('Agg') | |
| import requests | |
| import logging | |
| import re | |
| app = Flask(__name__) | |
| app.static_folder = 'static' | |
| def generate_3d_sphere(): | |
| # 获取请求中的代码 | |
| code = request.json['prompt'] | |
| # 判断代码是否以'''python开头和'''结尾 | |
| if code.startswith("```python") and code.endswith("```"): | |
| # 如果是,则删除开头和结尾的字符串 | |
| code = '\n'.join(code.split('\n')[1:-1]) | |
| elif code.startswith("'''python") and code.endswith("'''"): | |
| # 如果是,则删除开头和结尾的字符串 | |
| code = '\n'.join(code.split('\n')[1:-1]) | |
| # 在主线程中执行代码生成 3D 图像 | |
| fig = plt.figure() | |
| ax = fig.add_subplot(111, projection='3d') | |
| exec(code) | |
| # 将 3D 图像保存为 PNG 格式 | |
| 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) | |
| # 将图像保存到 'static' 目录 | |
| with open(save_path, 'wb') as f: | |
| f.write(buf.getvalue()) | |
| # 返回 JSON 格式的响应,包含图像的访问链接 | |
| img_url = f'https://mistpe-flask-spaces-whisper.hf.space/static/{img_filename}' | |
| return { | |
| "created": int(time.time()), | |
| "data": [ | |
| { | |
| "url": img_url | |
| } | |
| ] | |
| } | |
| def preprocess_prompt(prompt): | |
| # 判断提示内容是否为纯英文 | |
| if is_pure_english(prompt): | |
| processed_prompt = prompt | |
| else: | |
| # 使用正则表达式匹配 @startmindmap 和 @endmindmap 之间的内容 | |
| 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{mindmap_content}\n@endmindmap" | |
| else: | |
| processed_prompt = prompt | |
| return processed_prompt | |
| def is_pure_english(text): | |
| # 检查字符串中是否只包含英文字母、数字和一些特殊字符 | |
| allowed_chars = set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@\n*\-+ ') | |
| for char in text: | |
| if char not in allowed_chars: | |
| return False | |
| return True | |
| def generate_image(): | |
| # 获取前端请求中的数据 | |
| data = request.json | |
| logging.debug(f"Received request data: {data}") | |
| # 预处理 prompt | |
| processed_prompt = preprocess_prompt(data['prompt']) | |
| logging.debug(f"Processed prompt: {processed_prompt}") | |
| # 将数据发送到 https://plantumll.azurewebsites.net/coder 接口 | |
| response = requests.post('https://plantumll.azurewebsites.net/coder', data=processed_prompt) | |
| logging.debug(f"Response status code: {response.status_code}") | |
| logging.debug(f"Response text: {response.text}") | |
| # 处理响应数据 | |
| response_data = response.text | |
| created = 1589478378 # 假设这是一个固定值 | |
| url = f"https://plantumll.azurewebsites.net/png/{response_data}" | |
| # 构造返回给前端的数据 | |
| result = { | |
| "created": created, | |
| "data": [ | |
| { | |
| "url": url | |
| } | |
| ] | |
| } | |
| return jsonify(result) | |
| def transcribe_audio(): | |
| # 检查前端请求中的字段名,并获取音频文件 | |
| if 'file' in request.files: | |
| audio_file = request.files['file'] | |
| else: | |
| return jsonify({"error": "Missing audio file"}), 400 | |
| # 创建一个与后端服务器通信的请求 | |
| response = requests.post( | |
| 'https://mistpe-web.hf.space/asr?encode=true&task=transcribe&word_timestamps=false&output=json', | |
| files={'audio_file': (audio_file.filename, audio_file, 'audio/mpeg')}, | |
| headers={'accept': 'application/json'} | |
| ) | |
| # 检查后端服务器的响应状态 | |
| if response.status_code != 200: | |
| return jsonify({"error": "Error from ASR server"}), 500 | |
| # 将后端服务器的响应转发给前端 | |
| return jsonify(response.json()) | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860) | |