由网络副手--寻路人于2023.08.24 18:53:00发布在AIGC 生成相关代码 阅读835 评论0 喜欢0 一、目录结构 |--services |----__init__.py |---- imgToMusic.py |--venv |--app.py 二、代码 app.py ``` import services.imgToMusic from flask import Flask, request, jsonify from flask_cors import CORS app = Flask(__name__) CORS(app, resources=r'/*') @app.route("/hello") def hello_world(): return "Hello ~" @app.route("/image_to_text", methods=['POST']) def image_to_text(): reqData = request.json.get("data") resData = services.imgToMusic.imageBase64ToText(reqData) return resData @app.route("/imgtext_to_mpt", methods=['POST']) def imgtext_to_mpt(): reqData = request.json.get("imgText") resData = services.imgToMusic.imgtextToMpt(reqData) return resData @app.route("/mpt_to_music", methods=['POST']) def imgmpt_to_music(): reqData = request.json.get("img_mpt") resData = services.imgToMusic.imgMptToMusic(reqData) return resData ``` imgToMusic.py ``` import requests,json from gradio_client import Client #图片识别成文本URL ImgToTextUrl = "http://127.0.0.1:8881/run/predict" ImgTextToMptUrl = "http://127.0.0.1:8882/" ImgMptToMusicUrl = "http://127.0.0.1:8883/" #根据图片内容调用模型识别图片内容 def imageBase64ToText(imgBase64): response = requests.post(ImgToTextUrl, json={ "data": [ "data:image/jpeg;base64,"+imgBase64 ]}).json() imgData = response["data"][0] error = "" if imgData == "": error = "cant not trans img content! error!" return { "imgText" : imgData, "error" : error } def imgMptToMusic(musicPmt): client = Client(ImgMptToMusicUrl) musicGenRes = client.predict( "melody", # str in 'Model' Radio component musicPmt, "", # str (filepath or URL to file) in 'Melody Condition (optional)' Audio component 15, # int | float (numeric value between 1 and 120) in 'Duration' Slider component 250, # int | float in 'Top-k' Number component 1, # int | float in 'Top-p' Number component 1, # int | float in 'Temperature' Number component 3, # int | float in 'Classifier Free Guidance' Number component fn_index=1 ) return { "musicPath" : musicGenRes, "error" : "" } #解析音乐内容 def GetMusicPmt(result): lines = result.split('\n') for line in lines: if '"Prompt"' in line: return line def IsJson(jsonStr): try: jsonMap = json.loads(jsonStr) except ValueError: return False return jsonMap #图片内容生成prompt def imgtextToMpt(imgText): response = requests.post(ImgTextToMptUrl, json={ "prompt" : imgText, "history": [ [ "<>\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\n If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\n<>\n\n我给你输入一段文字,帮我描述成一首MusicGen音乐prompt,包含音乐元素。prompt 样例: “An 80s driving pop song with heavy drums and synth pads in the background”。输出的 prompt控制在30字内,不需要寒暄、打招呼等语气,直接输出prompt。输出Json格式: {Title: 结果标题, prompt : 结果prompt} 我输入的内容是“anime girl hugging her face in the snow with a picture of a man in the background”,没有更多信息可以提供,请创作即可,结尾不需要任何反问,不合适我会重新询问。", " Understood! Based on the input you provided, here is a MusicGen prompt for you:\n{Title: \"Wistful Winter Embrace\", prompt: \"An ethereal anime girl hugging her face in the snow, with a hazy picture of a man in the background, evoking a sense of longing and nostalgia.\"}\nI hope this prompt captures the mood and atmosphere you were looking for! Let me know if you have any other questions or if there's anything else I can help you with." ], [ "在prompt字段中内容应该是一段musicGen可生成音乐的描述,根据输入的内容来创造出来的", " Ah, I see! In that case, here's a MusicGen prompt based on your input:\n{Title: \"Snowy Serenity\", prompt: \"A gentle, atmospheric piece with a soothing melody and a subtle rhythm, evoking a sense of peacefulness and tranquility, perfect for a winter wonderland scene with an anime girl hugging her face in the snow.\"}\nI hope this prompt helps generate a beautiful and fitting piece of music for your anime girl in the snow scene! Let me know if you have any other questions or if there's anything else I can help you with." ], [ "你这个回答非常不错,prompt 不需要重复输入的关键词内容,可以替换为乐器、风格等元素", " Ah, I see! Yes, you're right. The `prompt` field in the MusicGen prompt can be replaced with more specific elements such as instruments, genres, or moods to help generate a more tailored piece of music. Here's an updated prompt based on your input:\n{Title: \"Winter's Embrace\", prompt: \"A delicate piano melody with a hint of melancholy, evoking a sense of longing and nostalgia, perfect for a snowy landscape with an anime girl hugging her face in the snow.\"}\nI hope this updated prompt helps generate a beautiful and fitting piece of music for your anime girl in the snow scene! Let me know if you have any other questions or if there's anything else I can help you with." ], [ "非常棒, 请在回答中仅输出 MusicGen prompt 的 json格式内容即可", " Of course! Here is the MusicGen prompt in JSON format:\n{\n\"Title\": \"Winter's Embrace\",\n\"Prompt\": \"A delicate piano melody with a hint of melancholy, evoking a sense of longing and nostalgia, perfect for a snowy landscape with an anime girl hugging her face in the snow.\"\n}\n\nI hope this helps! Let me know if you have any other questions." ], [ "以后我给出任何音乐描述都按照我们前面的约定格式返回", " Understood! I will always return the MusicGen prompt in the format we agreed upon, regardless of the music description you provide in the future. Please feel free to give me any music description, and I will generate the corresponding MusicGen prompt for you." ] ]}).json() imgMptStr = GetMusicPmt(response["response"]) if '{' not in imgMptStr: imgMptStr = "{"+imgMptStr+"}" imgMptMap = IsJson(imgMptStr) if imgMptMap == False: return { "imgMpt" : "", "error" : "图片识别失败~,请更换重试" } #imgMptMap = json.loads(imgMptStr) return { "imgMpt" : imgMptMap["Prompt"], "error" : "" } ``` 三、启动 nohup flask run -h 127.0.0.1 -p 8884 >> run.log & ``` #!/bin/bash cd /home/user/data/image_to_music/api_image_to_music source venv/bin/activate nohup flask run -h 127.0.0.1 -p 8884 >> run.log & deactivate ps -ef | grep 8884 ``` Nginx 转发 ``` server { listen 27777; root /home/user/data/image_to_music/web; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization'; if ($request_method = 'OPTIONS') { return 204; } location /api/ { rewrite ^.+api/?(.*)$ /$1 break; #include uwsgi_params; proxy_pass http://127.0.0.1:8884; proxy_set_header Host 127.0.0.1:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr; add_header X-Cache $upstream_cache_status; proxy_set_header X-Host $host:$server_port; proxy_set_header X-Scheme $scheme; proxy_connect_timeout 100s; proxy_read_timeout 86400s; proxy_send_timeout 100s; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } #access_log /home/user/data/image_to_music/logs/nginx_access.log; #error_log /home/user/data/image_to_music/logs/nginx_error.log; } ``` ------错误——---- 如何解决ubuntu系统下shell -source: not found错误? 请按以下步骤更改Shell的解释器: 执行ls -l /bin/sh命令,若得到结果/bin/sh -> dash,则说明Shell的解释器为dash。 执行dpkg-reconfigure dash命令,然后选择no。 重要 此步骤需要root权限。 再次执行ls -l /bin/sh命令,若得到结果/bin/sh -> bash,则说明成功更改Shell的解释器为bash。 赞 0 分享 赏 您可以选择一种方式赞助本站 支付宝扫码赞助 BraveDu 署名: 网络副手~寻路人