Spaces:
Runtime error
Runtime error
Commit ·
5fcebe4
1
Parent(s): 179ff34
Upload 2 files
Browse files
app.py
CHANGED
|
@@ -1,18 +1,72 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
|
|
|
|
|
|
| 3 |
openai.api_key = "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"
|
| 4 |
|
| 5 |
# 替换为您的模型ID
|
| 6 |
MODEL = "gpt-3.5-turbo"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def generate_response(prompt):
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
response = openai.ChatCompletion.create(
|
| 11 |
model=MODEL,
|
| 12 |
-
messages=message,
|
| 13 |
temperature=1)
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# 创建gradio接口
|
| 18 |
input_text = gr.inputs.Textbox(label="输入问题:")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
openai.api_key = "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"
|
| 6 |
|
| 7 |
# 替换为您的模型ID
|
| 8 |
MODEL = "gpt-3.5-turbo"
|
| 9 |
|
| 10 |
+
def check_path(path):
|
| 11 |
+
"""
|
| 12 |
+
检查文件是否存在
|
| 13 |
+
"""
|
| 14 |
+
if os.path.exists(path):
|
| 15 |
+
return True
|
| 16 |
+
else:
|
| 17 |
+
print(f"{path}文件不存在!!!")
|
| 18 |
+
return False
|
| 19 |
+
|
| 20 |
+
def check_create_path(path):
|
| 21 |
+
"""
|
| 22 |
+
检查文件路径是否存在,不存在则创建
|
| 23 |
+
"""
|
| 24 |
+
_path = os.path.dirname(path)
|
| 25 |
+
if not os.path.exists(_path):
|
| 26 |
+
os.makedirs(_path, exist_ok=True)
|
| 27 |
+
|
| 28 |
+
def read_json(path):
|
| 29 |
+
"""
|
| 30 |
+
支持json类型文件读取
|
| 31 |
+
"""
|
| 32 |
+
if check_path(path) == False:
|
| 33 |
+
return
|
| 34 |
+
|
| 35 |
+
data = None
|
| 36 |
+
data_type = path.split('.')[-1]
|
| 37 |
+
if data_type == "json":
|
| 38 |
+
data = json.load(open(path, 'r'))
|
| 39 |
+
else:
|
| 40 |
+
print(f"无法识别的文件后缀 {data_type}")
|
| 41 |
+
return data
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def save_json(path, data, do_type="w"):
|
| 45 |
+
"""
|
| 46 |
+
保存为json类型文件
|
| 47 |
+
"""
|
| 48 |
+
check_create_path(path)
|
| 49 |
+
if do_type == "w":
|
| 50 |
+
json.dump(data, open(path, 'w'), ensure_ascii=False)
|
| 51 |
+
else:
|
| 52 |
+
f = open(path, 'a')
|
| 53 |
+
f.write(json.dumps(data, ensure_ascii=False)+'\n')
|
| 54 |
+
f.close()
|
| 55 |
+
|
| 56 |
def generate_response(prompt):
|
| 57 |
+
try:
|
| 58 |
+
message = read_json('./log.json')
|
| 59 |
+
except:
|
| 60 |
+
pass
|
| 61 |
+
message.append({"role": "user", "content": prompt})
|
| 62 |
response = openai.ChatCompletion.create(
|
| 63 |
model=MODEL,
|
| 64 |
+
messages=message[-4:],
|
| 65 |
temperature=1)
|
| 66 |
+
message_ = response['choices'][0]['message']['content']
|
| 67 |
+
message.append({"role": "system", "content": message_})
|
| 68 |
+
save_json('./log.json', message)
|
| 69 |
+
return message_
|
| 70 |
|
| 71 |
# 创建gradio接口
|
| 72 |
input_text = gr.inputs.Textbox(label="输入问题:")
|
log.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[{"role": "user", "content": "asdf "}, {"role": "system", "content": "\n\nI'm sorry, I cannot provide a response as \"asdf\" is not a valid request or question. Can I assist you with anything specific?"}]
|