File size: 10,705 Bytes
c0fb54b
 
 
 
 
 
 
 
eef36cf
c0fb54b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6778e19
c0fb54b
 
 
 
 
 
 
 
 
6778e19
c0fb54b
 
 
 
 
 
 
 
6778e19
c0fb54b
 
 
 
 
 
 
 
 
 
 
6778e19
c0fb54b
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import time
import base64
from openai import OpenAI
import gradio
import google.generativeai as genai


def generation(prompt_text):
    a_key = "QUl6YVN5RGRJZXlsSnVUOERWMndlbXBfeE8wWUpmZEl4OFZ4S1FJ"
    genai.configure(api_key=str(base64.b64decode(a_key), encoding="utf-8"))

    generation_config = {"temperature": 0.9, "top_p": 1, "top_k": 1, "max_output_tokens": 2048}
    safety_settings = [
      {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
      {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
      {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
      {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}]

    model = genai.GenerativeModel(model_name="gemini-pro", generation_config=generation_config, safety_settings=safety_settings)

    response = model.generate_content(prompt_text)
    return response


chatgpt_model_type_normal = "gpt-3.5-turbo-instruct:普通对话型"
chatgpt_model_type_memory = "gpt-3.5-turbo-16k:记忆对话型"
chatgpt_model_type_gemini = "这是Google旗下原生多模态大模型 Gemini google-gemini-pro:普通对话型"
conversation_history = []
prompt_parts = [""]
def chatgpt(input_text, model_type, field):
    a_key = "c2stRFo3ZzRadXlzOEhMd2RWZnFYVWJUM0JsYmtGSkFPTm9PNHdPMmJZTTc5Tm5DTVQ5"
    client = OpenAI(api_key=str(base64.b64decode(a_key), encoding="utf-8"))

    if field != "":
        input_text = input_text + "\n" + "假设你扮演的人物角色或你专注的学术领域为:" + field

    if model_type == chatgpt_model_type_normal:
        response = client.completions.create(
            model="gpt-3.5-turbo-instruct",
            temperature=0.25,
            max_tokens=2500,
            frequency_penalty=0.5,
            presence_penalty=0.5,
            top_p=0.88,
            prompt=input_text
        )
        response_text = response.choices[0].text.strip()

    if model_type == chatgpt_model_type_memory:
        conversation_system = {"role": "system", "content": ""}
        conversation_user = {"role": "user", "content": input_text}
        if conversation_history == []:
            conversation_history.append(conversation_system)
            conversation_history.append(conversation_user)
        else:
            conversation_history.append(conversation_user)

        response = client.chat.completions.create(
            model="gpt-3.5-turbo-16k",
            temperature=0.25,
            max_tokens=5000,
            frequency_penalty=0.5,
            presence_penalty=0.5,
            top_p=0.88,
            messages=conversation_history,
        )
        response_text = response.choices[0].message.content

        conversation_system = {"role": "system", "content": response_text}
        conversation_history.append(conversation_system)

    if model_type == chatgpt_model_type_gemini:
        if prompt_parts[0] == "":
            prompt_parts[0] += input_text
        else:
            prompt_parts[0] += (" " + input_text)

        response_gemini = generation(prompt_parts)
        response_text = response_gemini.text
        prompt_parts[0] += (" " + response_text)

    return response_text


dall_e_model_type_2 = "Dall-E-2:较快 | 较差"
dall_e_model_type_3 = "Dall-E-3:较慢 | 较好"
def dall_e(input_text, add_text, model_type):
    a_key = "c2stRFo3ZzRadXlzOEhMd2RWZnFYVWJUM0JsYmtGSkFPTm9PNHdPMmJZTTc5Tm5DTVQ5"
    client = OpenAI(api_key=str(base64.b64decode(a_key), encoding="utf-8"))

    complete_text = input_text + "\n" + "增加图片风格的细节或场景的细节:" + add_text
    if model_type == dall_e_model_type_2:
        model_select = "dall-e-2"
    if model_type == dall_e_model_type_3:
        model_select = "dall-e-3"

    response = client.images.generate(
        model=model_select,
        prompt=complete_text,
        size="1024x1024",
        quality="standard",
        n=1,
    )
    image_url = response.data[0].url
    return image_url


def whisper(audio_file):
    a_key = "c2stRFo3ZzRadXlzOEhMd2RWZnFYVWJUM0JsYmtGSkFPTm9PNHdPMmJZTTc5Tm5DTVQ5"
    client = OpenAI(api_key=str(base64.b64decode(a_key), encoding="utf-8"))
    audio_file = open(audio_file, "rb")
    transcript = client.audio.transcriptions.create(model="whisper-1", file=audio_file, response_format="text")

    return transcript


tts_model_type_1 = "tts-1:较快 | 较差"
tts_model_type_1_hd = "tts-1-hd:较慢 | 较好"
tts_gender_male = "男生声音:Alloy"
tts_gender_female = "女生声音:Nova"
def tts(input_text, model_type, gender):
    a_key = "c2stRFo3ZzRadXlzOEhMd2RWZnFYVWJUM0JsYmtGSkFPTm9PNHdPMmJZTTc5Tm5DTVQ5"
    client = OpenAI(api_key=str(base64.b64decode(a_key), encoding="utf-8"))

    timestamp = time.time()
    speech_file_path = "tts_" + str(timestamp) + ".mp3"

    if model_type == "tts-1:较快 | 较差":
        model_select = "tts-1"
    if model_type == "tts-1-hd:较慢 | 较好":
        model_select = "tts-1-hd"
    if gender == "男生声音:Alloy":
        voice_select = "alloy"
    if gender == "女生声音:Nova":
        voice_select = "nova"

    response = client.audio.speech.create(
        model=model_select,
        voice=voice_select,
        input=input_text,
    )
    response.stream_to_file(speech_file_path)
    return speech_file_path, whisper(speech_file_path)


def demo():
    article_thanks = "<br> 🔅Thanks Python. &nbsp;&nbsp;  🔅Thanks OpenAI. &nbsp;&nbsp;  🔅Thanks Gradio. &nbsp;&nbsp;  🔅Thanks Myself. &nbsp;&nbsp;  🔅Thank you !"
    chat_demo = gradio.Interface(fn=chatgpt,
                                 inputs=[gradio.Textbox(label="💢(必须) ⚡你的对话", lines=3, placeholder="输入内容", show_copy_button=True),
                                         gradio.Radio([chatgpt_model_type_normal, chatgpt_model_type_memory, chatgpt_model_type_gemini],
                                                      label="⚡选择GPT模型", value=chatgpt_model_type_normal),
                                         gradio.Textbox(label="⚡人物角色 / 学术领域(选择 #记忆对话型 生效)", lines=1,
                                                        placeholder="例如:马斯克;人工智能")],
                                 outputs=gradio.Textbox(label="⚡ GPT 对话生成", lines=11,
                                                        show_copy_button=True,
                                                        placeholder="ChatGPT - 是未成熟的AGI智能工具 - 无法保证回答正确 !仅供参考 😊"),
                                 title="<br>MyDIY_Resume | Gradio:🤖 Chat-with-GPT3.5",
                                 description="✅ 操作指南:在左边的文本框中输入内容,再选择对话的GPT模型类型,如果选择为角色型则可以为GPT设定一种角色或领域作为回答的方向 !😁",
                                 article=article_thanks)

    image_demo = gradio.Interface(fn=dall_e,
                                  inputs=[gradio.Textbox(label="💢(必须) ⚡描述图片", lines=2, placeholder="输入内容", show_copy_button=True),
                                          gradio.Textbox(label="⚡描述细节", lines=1, placeholder="例如:蔚蓝;科幻", show_copy_button=True),
                                          gradio.Radio([dall_e_model_type_2, dall_e_model_type_3],
                                                       label="⚡选择Dall-E模型", value=dall_e_model_type_2)],
                                  outputs=gradio.Image(label="⚡Dell-E 图片生成"),
                                  title="<br>MyDIY_Resume | Gradio:🤖 Dall-E-Generate-Image",
                                  description="✅ 操作指南:在左边的文本框中输入需要生成图片的内容,再选择生成图片的Dall-E模型,需要注意Dall-E-3模型的请求更容易受到限制 !😁",
                                  article=article_thanks)

    stt_demo = gradio.Interface(fn=whisper,
                                inputs=[gradio.Audio(label="💢(必须) ⚡上传音频文件", type="filepath")],
                                outputs=gradio.Textbox(label="⚡Whisper 文字生成", lines=10,
                                                       placeholder="Whisper-v1 - 是初代版本模型 - 无法保证转换正确 !仅供参考 😊",
                                                       show_copy_button=True),
                                title="<br>MyDIY_Resume | Gradio:🤖 Whisper-Speech-to-Text",
                                description="✅ 操作指南:点击左边的音频框进行本地音频文件选取,或者直接将音频文件拖拉到当前音频框,默认的音频转文本模型是Whisper-v1 !😁",
                                article=article_thanks)

    tts_demo = gradio.Interface(fn=tts,
                                inputs=[gradio.Textbox(label="💢(必须) ⚡你的文本", lines=3, placeholder="输入内容", show_copy_button=True),
                                        gradio.Radio([tts_model_type_1, tts_model_type_1_hd], label="选择TTS模型", value=tts_model_type_1),
                                        gradio.Radio([tts_gender_male, tts_gender_female], label="选择声音性别", value=tts_gender_male)],
                                outputs=[gradio.Audio(label="⚡TTS 音频生成"),
                                         gradio.Textbox(label="生成的音频再经过 Whisper 二次检验", lines=5,
                                                        placeholder="生成的音频再次经过 Whisper 转换用于验证TTS的准确性 !仅供参考 😊",
                                                        show_copy_button=True)],
                                title="<br>MyDIY_Resume | Gradio:🤖 TTS-Text-to-Speech",
                                description="✅ 操作指南:在左边的文本框中输入需要转音频的内容,再选择生成音频的TTS模型,两个模型实际差异不大,最后为模型选择输出男声还是女声 !😁",
                                article=article_thanks)

    GUI = gradio.TabbedInterface([chat_demo, image_demo, stt_demo, tts_demo],
                                 ["1️⃣ Chat-with-GPT:对话学习", "2️⃣ Generate-Image:生成图片", "3️⃣ Speech-to-Text:语音转文本", "4️⃣ Text-to-Speech:文本转语音"],
                                 css="footer {visibility: hidden}")

    GUI.launch(debug=True, share=False)


if __name__ == "__main__":
    demo()