Spaces:
Runtime error
Runtime error
Download app.py from lancecrazy/zhy_demo: direct link, hf CLI and curl.
- Browser
- Download file 2.23 kB
-
https://huggingface.co/spaces/lancecrazy/zhy_demo/resolve/main/app.py
- Command line
-
hf download hf://spaces/lancecrazy/zhy_demo/app.py
-
curl -L -o app.py https://huggingface.co/spaces/lancecrazy/zhy_demo/resolve/main/app.py
2.23 kB
| import gradio as gr | |
| import openai | |
| import json | |
| import os | |
| try: | |
| print(zhy_key) | |
| except: | |
| print('no key') | |
| openai.api_key = "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" | |
| # 替换为您的模型ID | |
| MODEL = "gpt-3.5-turbo" | |
| def check_path(path): | |
| """ | |
| 检查文件是否存在 | |
| """ | |
| if os.path.exists(path): | |
| return True | |
| else: | |
| print(f"{path}文件不存在!!!") | |
| return False | |
| def check_create_path(path): | |
| """ | |
| 检查文件路径是否存在,不存在则创建 | |
| """ | |
| _path = os.path.dirname(path) | |
| if not os.path.exists(_path): | |
| os.makedirs(_path, exist_ok=True) | |
| def read_json(path): | |
| """ | |
| 支持json类型文件读取 | |
| """ | |
| if check_path(path) == False: | |
| return | |
| data = None | |
| data_type = path.split('.')[-1] | |
| if data_type == "json": | |
| data = json.load(open(path, 'r')) | |
| else: | |
| print(f"无法识别的文件后缀 {data_type}") | |
| return data | |
| def save_json(path, data, do_type="w"): | |
| """ | |
| 保存为json类型文件 | |
| """ | |
| check_create_path(path) | |
| if do_type == "w": | |
| json.dump(data, open(path, 'w'), ensure_ascii=False) | |
| else: | |
| f = open(path, 'a') | |
| f.write(json.dumps(data, ensure_ascii=False)+'\n') | |
| f.close() | |
| def generate_response(prompt): | |
| try: | |
| message = read_json('./log.json') | |
| except: | |
| pass | |
| print('prompt: ', prompt) | |
| message.append({"role": "user", "content": prompt}) | |
| response = openai.ChatCompletion.create( | |
| model=MODEL, | |
| messages=message[-1:] + [{"role": "system", "content": '你的表达很有逻辑并且不说废话,得出的每一个结论都有依据以及思考'}], | |
| temperature=1) | |
| message_ = response['choices'][0]['message']['content'] | |
| message.append({"role": "assistant", "content": message_}) | |
| save_json('./log.json', message) | |
| print('response: ', message_) | |
| return message_ | |
| # 创建gradio接口 | |
| input_text = gr.inputs.Textbox(label="输入问题:") | |
| output_text = gr.outputs.Textbox(label="回答:") | |
| interface = gr.Interface(fn=generate_response, inputs=input_text, outputs=output_text, title="------- 0.o ------") | |
| interface.launch() | |
| # print(share_url) | |