AUST001 commited on
Commit
286c55e
·
1 Parent(s): 8b1180c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
14
+ global client
15
+ context.append({"role": "user", "content": user_question})
16
+ for chunk in client.send_message("a2_100k", 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("a2_100k") # 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):
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)
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
+ demo = gr.Interface(
74
+ fn=greet,
75
+ inputs=[
76
+ gr.Textbox(lines=1, label='请输入用户ID', placeholder='请输入用户ID'),
77
+ gr.Textbox(lines=1, label='请输入你的专属密钥', placeholder='请输入你的专属密钥'),
78
+ gr.Textbox(lines=15, label='请输入问题', placeholder='请输入您的问题'),
79
+ gr.Checkbox(label='清空聊天记录', default=False)
80
+ ],
81
+ outputs=[
82
+ gr.Textbox(lines=1, label='聊天记录状态', placeholder='等待清空聊天记录'),
83
+ gr.Textbox(lines=23, label='AI回答', placeholder='等待AI回答')
84
+ ],
85
+ title="Claude-instant-100k",
86
+ description="""
87
+ 1.使用说明:
88
+ 请输入您的问题,AI助手会给出回答。
89
+ 支持连续对话,可以记录对话历史。
90
+ 重新开始对话勾选清空聊天记录,输出清空成功表示重新开启对话。
91
+ 2.特别警告:
92
+ 为了防止用户数据混乱,请自定义用户ID。
93
+ 理论上如果被别人知道自己的ID,那么别人可以查看自己的历史对话,对此你可以选择在对话结束后清除对话记录。
94
+ 3.作者的GPT4网页导航网站链接如下:http://aust001.pythonanywhere.com/ -> 专属密钥进群获取
95
+ """
96
+ )
97
+
98
+ if __name__ == "__main__":
99
+ demo.launch()