|
2 | 2 | from http import HTTPStatus |
3 | 3 | from typing import Dict |
4 | 4 |
|
5 | | -from dashscope import ImageSynthesis |
| 5 | +from dashscope import ImageSynthesis, MultiModalConversation |
6 | 6 | from django.utils.translation import gettext |
7 | 7 | from langchain_community.chat_models import ChatTongyi |
8 | 8 | from langchain_core.messages import HumanMessage |
@@ -46,17 +46,48 @@ def check_auth(self): |
46 | 46 | chat.invoke([HumanMessage([{"type": "text", "text": gettext('Hello')}])]) |
47 | 47 |
|
48 | 48 | def generate_image(self, prompt: str, negative_prompt: str = None): |
49 | | - rsp = ImageSynthesis.call(api_key=self.api_key, |
50 | | - model=self.model_name, |
51 | | - base_url='https://dashscope.aliyuncs.com/compatible-mode/v1', |
52 | | - prompt=prompt, |
53 | | - negative_prompt=negative_prompt, |
54 | | - **self.params) |
55 | | - file_urls = [] |
56 | | - if rsp.status_code == HTTPStatus.OK: |
57 | | - for result in rsp.output.results: |
58 | | - file_urls.append(result.url) |
59 | | - else: |
60 | | - maxkb_logger.error('sync_call Failed, status_code: %s, code: %s, message: %s' % |
61 | | - (rsp.status_code, rsp.code, rsp.message)) |
62 | | - return file_urls |
| 49 | + if self.model_name.startswith("wan"): |
| 50 | + rsp = ImageSynthesis.call(api_key=self.api_key, |
| 51 | + model=self.model_name, |
| 52 | + base_url='https://dashscope.aliyuncs.com/compatible-mode/v1', |
| 53 | + prompt=prompt, |
| 54 | + negative_prompt=negative_prompt, |
| 55 | + **self.params) |
| 56 | + file_urls = [] |
| 57 | + if rsp.status_code == HTTPStatus.OK: |
| 58 | + for result in rsp.output.results: |
| 59 | + file_urls.append(result.url) |
| 60 | + else: |
| 61 | + maxkb_logger.error('sync_call Failed, status_code: %s, code: %s, message: %s' % |
| 62 | + (rsp.status_code, rsp.code, rsp.message)) |
| 63 | + return file_urls |
| 64 | + elif self.model_name.startswith("qwen"): |
| 65 | + messages = [ |
| 66 | + { |
| 67 | + "role": "user", |
| 68 | + "content": [ |
| 69 | + { |
| 70 | + "type": "text", |
| 71 | + "text": prompt |
| 72 | + } |
| 73 | + ] |
| 74 | + } |
| 75 | + ] |
| 76 | + rsp = MultiModalConversation.call( |
| 77 | + api_key=self.api_key, |
| 78 | + model=self.model_name, |
| 79 | + messages=messages, |
| 80 | + result_format='message', |
| 81 | + base_url='https://dashscope.aliyuncs.com/v1', |
| 82 | + stream=False, |
| 83 | + negative_prompt=negative_prompt, |
| 84 | + **self.params |
| 85 | + ) |
| 86 | + file_urls = [] |
| 87 | + if rsp.status_code == HTTPStatus.OK: |
| 88 | + for result in rsp.output.choices: |
| 89 | + file_urls.append(result.message.content[0].get('image')) |
| 90 | + else: |
| 91 | + maxkb_logger.error('sync_call Failed, status_code: %s, code: %s, message: %s' % |
| 92 | + (rsp.status_code, rsp.code, rsp.message)) |
| 93 | + return file_urls |
0 commit comments