From 10f177adacceb3b553d1e1d0ff5096afe58fab78 Mon Sep 17 00:00:00 2001 From: yrk <2493404415@qq.com> Date: Wed, 21 May 2025 10:37:40 +0800 Subject: [PATCH 1/4] Add support for ModelScope API --- app/services/llm.py | 36 ++++++++++++++++++++++++++++++++++++ config.example.toml | 8 ++++++++ webui/Main.py | 14 ++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/app/services/llm.py b/app/services/llm.py index 6c954a8..34139eb 100644 --- a/app/services/llm.py +++ b/app/services/llm.py @@ -74,6 +74,12 @@ def _generate_response(prompt: str) -> str: base_url = config.app.get("deepseek_base_url") if not base_url: base_url = "https://api.deepseek.com" + elif llm_provider == "modelscope": + api_key = config.app.get("modelscope_api_key") + model_name = config.app.get("modelscope_model_name") + base_url = config.app.get("modelscope_base_url") + if not base_url: + base_url = "https://api-inference.modelscope.cn/v1/" elif llm_provider == "ernie": api_key = config.app.get("ernie_api_key") secret_key = config.app.get("ernie_secret_key") @@ -264,6 +270,34 @@ def _generate_response(prompt: str) -> str: api_version=api_version, azure_endpoint=base_url, ) + + if llm_provider == "modelscope": + content = '' + client = OpenAI( + api_key=api_key, + base_url=base_url, + ) + response = client.chat.completions.create( + model=model_name, + messages=[{"role": "user", "content": prompt}], + extra_body={"enable_thinking": False}, + stream=True + ) + if response: + for chunk in response: + if not chunk.choices: + continue + delta = chunk.choices[0].delta + if delta and delta.content: + content += delta.content + + if not content.strip(): + raise ValueError("Empty content in stream response") + + return content.replace("\n", "") + else: + raise Exception(f"[{llm_provider}] returned an empty response") + else: client = OpenAI( api_key=api_key, @@ -399,6 +433,8 @@ Please note that you must use English for generating video search terms; Chinese for i in range(_max_retries): try: response = _generate_response(prompt) + print(response) + print('1111111111') if "Error: " in response: logger.error(f"failed to generate video script: {response}") return response diff --git a/config.example.toml b/config.example.toml index ffe250c..a7f8098 100644 --- a/config.example.toml +++ b/config.example.toml @@ -99,6 +99,14 @@ deepseek_api_key = "" deepseek_base_url = "https://api.deepseek.com" deepseek_model_name = "deepseek-chat" + +########## ModelScope API Key +# Visit https://modelscope.cn/docs/model-service/API-Inference/intro to get your API key +# And note that you need to bind your Alibaba Cloud account before using the API. +modelscope_api_key = "" +modelscope_base_url = "https://api-inference.modelscope.cn/v1/" +modelscope_model_name = "Qwen/Qwen3-32B" + # Subtitle Provider, "edge" or "whisper" # If empty, the subtitle will not be generated subtitle_provider = "edge" diff --git a/webui/Main.py b/webui/Main.py index 1b55abe..8b0a401 100644 --- a/webui/Main.py +++ b/webui/Main.py @@ -231,6 +231,7 @@ if not config.app.get("hide_config", False): "Azure", "Qwen", "DeepSeek", + "ModelScope", "Gemini", "Ollama", "G4f", @@ -373,6 +374,19 @@ if not config.app.get("hide_config", False): - **Model Name**: 固定为 deepseek-chat """ + if llm_provider == "modelscope": + if not llm_model_name: + llm_model_name = "Qwen/Qwen3-32B" + if not llm_base_url: + llm_base_url = "https://api-inference.modelscope.cn/v1/" + with llm_helper: + tips = """ + ##### ModelScope 配置说明 + - **API Key**: [点击到官网申请](https://modelscope.cn/docs/model-service/API-Inference/intro) + - **Base Url**: 固定为 https://api-inference.modelscope.cn/v1/ + - **Model Name**: 比如 Qwen/Qwen3-32B,[点击查看模型列表](https://modelscope.cn/models?filter=inference_type&page=1) + """ + if llm_provider == "ernie": with llm_helper: tips = """ From b36caf63cf5979c6461abd00dc7aba4e5893e812 Mon Sep 17 00:00:00 2001 From: yrk <2493404415@qq.com> Date: Wed, 21 May 2025 10:40:55 +0800 Subject: [PATCH 2/4] Modify llm.py --- app/services/llm.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/services/llm.py b/app/services/llm.py index 34139eb..e98bc1e 100644 --- a/app/services/llm.py +++ b/app/services/llm.py @@ -433,8 +433,6 @@ Please note that you must use English for generating video search terms; Chinese for i in range(_max_retries): try: response = _generate_response(prompt) - print(response) - print('1111111111') if "Error: " in response: logger.error(f"failed to generate video script: {response}") return response From 18be15f4461ccb8d2ec17be89faefa40f25a7c04 Mon Sep 17 00:00:00 2001 From: yrk <2493404415@qq.com> Date: Wed, 21 May 2025 11:01:37 +0800 Subject: [PATCH 3/4] update config.example.toml --- config.example.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/config.example.toml b/config.example.toml index a7f8098..5f77f42 100644 --- a/config.example.toml +++ b/config.example.toml @@ -30,6 +30,7 @@ pixabay_api_keys = [] # oneapi # cloudflare # ernie (文心一言) +# modelscope (魔搭社区) llm_provider = "openai" ########## Pollinations AI Settings From 93142917486ed5e681d46695998c639bb2242b6c Mon Sep 17 00:00:00 2001 From: yrk <2493404415@qq.com> Date: Wed, 21 May 2025 11:26:17 +0800 Subject: [PATCH 4/4] Update README.md --- README-en.md | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README-en.md b/README-en.md index 7c16f2d..4590ae3 100644 --- a/README-en.md +++ b/README-en.md @@ -65,7 +65,7 @@ Picwish focuses on the **image processing field**, providing a rich set of **ima supports `subtitle outlining` - [x] Supports **background music**, either random or specified music files, with adjustable `background music volume` - [x] Video material sources are **high-definition** and **royalty-free**, and you can also use your own **local materials** -- [x] Supports integration with various models such as **OpenAI**, **Moonshot**, **Azure**, **gpt4free**, **one-api**, **Qwen**, **Google Gemini**, **Ollama**, **DeepSeek**, **ERNIE**, **Pollinations** and more +- [x] Supports integration with various models such as **OpenAI**, **Moonshot**, **Azure**, **gpt4free**, **one-api**, **Qwen**, **Google Gemini**, **Ollama**, **DeepSeek**, **ERNIE**, **Pollinations**, **ModelScope** and more ### Future Plans 📅 diff --git a/README.md b/README.md index 7812761..df23b08 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ - [x] 支持 **字幕生成**,可以调整 `字体`、`位置`、`颜色`、`大小`,同时支持`字幕描边`设置 - [x] 支持 **背景音乐**,随机或者指定音乐文件,可设置`背景音乐音量` - [x] 视频素材来源 **高清**,而且 **无版权**,也可以使用自己的 **本地素材** -- [x] 支持 **OpenAI**、**Moonshot**、**Azure**、**gpt4free**、**one-api**、**通义千问**、**Google Gemini**、**Ollama**、**DeepSeek**、 **文心一言**, **Pollinations** 等多种模型接入 +- [x] 支持 **OpenAI**、**Moonshot**、**Azure**、**gpt4free**、**one-api**、**通义千问**、**Google Gemini**、**Ollama**、**DeepSeek**、 **文心一言**, **Pollinations**、**ModelScope** 等多种模型接入 - 中国用户建议使用 **DeepSeek** 或 **Moonshot** 作为大模型提供商(国内可直接访问,不需要VPN。注册就送额度,基本够用)