from gradio_client import Client, file, handle_file
import asyncio
import base64
import io
import json
import os
from datetime import datetime
from nonebot import logger
from PIL import Image, ImageOps, ImageDraw, ImageFont
from nonebot.internal.params import ArgPlainText
from nonebot.params import CommandArg
from nonebot.typing import T_State
from playwright.async_api import async_playwright
from nonebot import on_command
from nonebot.adapters.onebot.v11 import MessageEvent, Bot, MessageSegment, GroupMessageEvent, Message
from nonebot.plugin import PluginMetadata
from typing import Optional, Union
from nonebot.matcher import Matcher

__plugin_meta__ = PluginMetadata(
    name="语音合成",
    description="太好玩了",
    usage="语音合成",
    type="application",
    extra={

    }
)

voice = on_command("voice", aliases={"语音合成"})


@voice.handle()
async def make_voice(ev: MessageEvent, rgs: Message = CommandArg()):
    text = rgs.extract_plain_text()
    if text is None:
        await voice.finish("缺少文本")
        return
    await voice.send("生成中...请耐心等待")
    client = Client("http://localhost:9872/")
    result = client.predict(
        text=f"{text}",
        text_lang="中文",
        ref_audio_path=handle_file(
            "D:\RVC\LJ\雷军_年度演讲.wav"),
        aux_ref_audio_paths=[],
        prompt_text="全球今天的消费者买手机，每六个人至少有一个人买的是小米手机",
        prompt_lang="中文",
        top_k=15,
        top_p=1,
        temperature=1,
        text_split_method="按标点符号切",
        batch_size=20,
        speed_factor=1,
        ref_text_free=False,
        split_bucket=True,
        fragment_interval=0.25,
        seed=-1,
        keep_random=True,
        parallel_infer=True,
        repetition_penalty=1.35,
        api_name="/inference"
    )
    audio_path = result[0]
    message = MessageSegment.record(audio_path)
    await voice.finish(message)
