Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pickle import NONE
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
import urllib.request
|
| 5 |
+
import openai
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import random
|
| 8 |
+
import poe
|
| 9 |
+
|
| 10 |
+
client = None
|
| 11 |
+
user_contexts = {}
|
| 12 |
+
|
| 13 |
+
def get_assistant_response(user_question, context, model_name):
|
| 14 |
+
global client, models
|
| 15 |
+
context.append({"role": "user", "content": user_question})
|
| 16 |
+
for chunk in client.send_message(models[model_name], context): # capybara
|
| 17 |
+
pass
|
| 18 |
+
# print(chunk["text"])
|
| 19 |
+
assistant_response = chunk["text"]
|
| 20 |
+
context.append({"role": "assistant", "content": assistant_response})
|
| 21 |
+
client.send_chat_break(models[model_name]) # capybara
|
| 22 |
+
return assistant_response
|
| 23 |
+
|
| 24 |
+
def generate_image_url(prompt):
|
| 25 |
+
response = openai.Image.create(
|
| 26 |
+
prompt=prompt,
|
| 27 |
+
n=1, # 生成1张图片
|
| 28 |
+
size="512x512", # 图像大小
|
| 29 |
+
)
|
| 30 |
+
image_url = response["data"][0]["url"]
|
| 31 |
+
return image_url
|
| 32 |
+
|
| 33 |
+
def greet(user_id, api_key, user_question, clear_history, model_name):
|
| 34 |
+
global client
|
| 35 |
+
if len(api_key)>5:
|
| 36 |
+
client = poe.Client(api_key)
|
| 37 |
+
global user_contexts
|
| 38 |
+
if user_id not in user_contexts:
|
| 39 |
+
user_contexts[user_id] = [
|
| 40 |
+
{"role": "system", "content": "你是一个聪明的AI助手。请参考对话记录,回答用户的最后一个问题,无需做多余的解释,更不要强调对话历史的事情"},
|
| 41 |
+
{"role": "user", "content": "你会说中文吗?"},
|
| 42 |
+
{"role": "assistant", "content": "是的,我可以说中文。"}
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
context = user_contexts[user_id]
|
| 46 |
+
|
| 47 |
+
if clear_history:
|
| 48 |
+
context = [
|
| 49 |
+
{"role": "system", "content": "你是一个聪明的AI助手。请参考对话记录,回答用户的最后一个问题,无需做多余的解释,更不要强调对话历史的事情"},
|
| 50 |
+
{"role": "user", "content": "你会说中文吗?"},
|
| 51 |
+
{"role": "assistant", "content": "是的,我可以说中文。"}
|
| 52 |
+
]
|
| 53 |
+
user_contexts[user_id] = context
|
| 54 |
+
return '清空成功', '保持聊天记录', np.ones((5,5))
|
| 55 |
+
else:
|
| 56 |
+
# 如果user提问包含生成图像的特定指令(这里我们使用“生成图片:”作为示例)
|
| 57 |
+
if user_question.startswith("生成图片:") or user_question.startswith("生成图片:"):
|
| 58 |
+
image_prompt = user_question[5:] # 提取用于生成图片的文本
|
| 59 |
+
image_url = generate_image_url(image_prompt)
|
| 60 |
+
resp = urllib.request.urlopen(image_url)
|
| 61 |
+
image = np.asarray(bytearray(resp.read()), dtype="uint8")
|
| 62 |
+
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
|
| 63 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 64 |
+
# return image
|
| 65 |
+
return '', '图片已生成', image
|
| 66 |
+
get_assistant_response(user_question, context, model_name)
|
| 67 |
+
prompt = ""
|
| 68 |
+
|
| 69 |
+
for item in context[3:]:
|
| 70 |
+
prompt += item["role"] + ": " + item["content"] + "\n"
|
| 71 |
+
return '', prompt, np.ones((5,5))
|
| 72 |
+
|
| 73 |
+
models = {
|
| 74 |
+
"GPT-4": "beaver",
|
| 75 |
+
"Claude-instant-100k": "a2_100k",
|
| 76 |
+
"Claude+": "a2_2",
|
| 77 |
+
"ChatGPT": "chinchilla",
|
| 78 |
+
"Claude-instant": "a2",
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
demo = gr.Interface(
|
| 82 |
+
fn=greet,
|
| 83 |
+
inputs=[
|
| 84 |
+
gr.Textbox(lines=1, label='请输入用户ID', placeholder='请输入用户ID'),
|
| 85 |
+
gr.Textbox(lines=1, label='请输入你的专属密钥', placeholder='请输入你的专属密钥'),
|
| 86 |
+
gr.Textbox(lines=15, label='请输入问题', placeholder='请输入您的问题'),
|
| 87 |
+
gr.Checkbox(label='清空聊天记录', default=False),
|
| 88 |
+
gr.Radio(choices=list(models.keys()), label="选择模型")
|
| 89 |
+
],
|
| 90 |
+
outputs=[
|
| 91 |
+
gr.Textbox(lines=1, label='聊天记录状态', placeholder='等待清空聊天记录'),
|
| 92 |
+
gr.Textbox(lines=25, label='AI回答', placeholder='等待AI回答')
|
| 93 |
+
],
|
| 94 |
+
title="ChatALL",
|
| 95 |
+
description="""
|
| 96 |
+
1.使用说明:
|
| 97 |
+
请输入您的问题,AI助手会给出回答。
|
| 98 |
+
支持连续对话,可以记录对话历史。
|
| 99 |
+
重新开始对话勾选清空聊天记录,输出清空成功表示重新开启对话。
|
| 100 |
+
2.特别警告:
|
| 101 |
+
为了防止用户数据混乱,请自定义用户ID。
|
| 102 |
+
理论上如果被别人知道自己的ID,那么别人可以查看自己的历史对话,对此你可以选择在对话结束后清除对话记录。
|
| 103 |
+
3.作者的GPT4网页导航网站链接如下:http://aust001.pythonanywhere.com/ -> 专属密钥进群获取
|
| 104 |
+
"""
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
demo.launch()
|