Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
1 |
import openai
|
2 |
import gradio as gr
|
3 |
import random
|
@@ -17,6 +20,15 @@ def get_assistant_response(user_question, context):
|
|
17 |
context.append({"role": "assistant", "content": assistant_response})
|
18 |
return assistant_response
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def greet(user_id, user_question, clear_history):
|
21 |
global user_contexts
|
22 |
if user_id not in user_contexts:
|
@@ -35,14 +47,24 @@ def greet(user_id, user_question, clear_history):
|
|
35 |
{"role": "assistant", "content": "是的,我可以说中文。"}
|
36 |
]
|
37 |
user_contexts[user_id] = context
|
38 |
-
return '清空成功', '保持聊天记录'
|
39 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
get_assistant_response(user_question, context)
|
41 |
prompt = ""
|
42 |
|
43 |
for item in context[3:]:
|
44 |
prompt += item["role"] + ": " + item["content"] + "\n"
|
45 |
-
return '', prompt
|
46 |
|
47 |
demo = gr.Interface(
|
48 |
fn=greet,
|
@@ -53,7 +75,8 @@ demo = gr.Interface(
|
|
53 |
],
|
54 |
outputs=[
|
55 |
gr.Textbox(lines=1, label='聊天记录状态', placeholder='等待清空聊天记录'),
|
56 |
-
gr.Textbox(lines=20, label='AI回答', placeholder='等待AI回答')
|
|
|
57 |
],
|
58 |
title="AI助手",
|
59 |
description="请输入您的问题,AI助手会给出回答。支持连续对话,可以记录对话历史。重新开始对话勾选清空聊天记录,输出清空成功表示重新开启对话。"
|
|
|
1 |
+
import numpy as np
|
2 |
+
import cv2
|
3 |
+
import urllib.request
|
4 |
import openai
|
5 |
import gradio as gr
|
6 |
import random
|
|
|
20 |
context.append({"role": "assistant", "content": assistant_response})
|
21 |
return assistant_response
|
22 |
|
23 |
+
def generate_image_url(prompt):
|
24 |
+
response = openai.Image.create(
|
25 |
+
prompt=prompt,
|
26 |
+
n=1, # 生成1张图片
|
27 |
+
size="512x512", # 图像大小
|
28 |
+
)
|
29 |
+
image_url = response["data"][0]["url"]
|
30 |
+
return image_url
|
31 |
+
|
32 |
def greet(user_id, user_question, clear_history):
|
33 |
global user_contexts
|
34 |
if user_id not in user_contexts:
|
|
|
47 |
{"role": "assistant", "content": "是的,我可以说中文。"}
|
48 |
]
|
49 |
user_contexts[user_id] = context
|
50 |
+
return '清空成功', '保持聊天记录', np.ones((5,5))
|
51 |
else:
|
52 |
+
# 如果user提问包含生成图像的特定指令(这里我们使用“生成图片:”作为示例)
|
53 |
+
if user_question.startswith("生成图片:"):
|
54 |
+
image_prompt = user_question[5:] # 提取用于生成图片的文本
|
55 |
+
image_url = generate_image_url(image_prompt)
|
56 |
+
resp = urllib.request.urlopen(image_url)
|
57 |
+
image = np.asarray(bytearray(resp.read()), dtype="uint8")
|
58 |
+
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
|
59 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
60 |
+
# return image
|
61 |
+
return '', '图片已生成', image
|
62 |
get_assistant_response(user_question, context)
|
63 |
prompt = ""
|
64 |
|
65 |
for item in context[3:]:
|
66 |
prompt += item["role"] + ": " + item["content"] + "\n"
|
67 |
+
return '', prompt, np.ones((5,5))
|
68 |
|
69 |
demo = gr.Interface(
|
70 |
fn=greet,
|
|
|
75 |
],
|
76 |
outputs=[
|
77 |
gr.Textbox(lines=1, label='聊天记录状态', placeholder='等待清空聊天记录'),
|
78 |
+
gr.Textbox(lines=20, label='AI回答', placeholder='等待AI回答'),
|
79 |
+
gr.Image(label='等待图片生成')
|
80 |
],
|
81 |
title="AI助手",
|
82 |
description="请输入您的问题,AI助手会给出回答。支持连续对话,可以记录对话历史。重新开始对话勾选清空聊天记录,输出清空成功表示重新开启对话。"
|