Spaces:
Running
Running
# !/usr/bin/env python | |
# -*-coding:utf-8 -*- | |
""" | |
# File : claude2_xiaolv_api_file_chat.py | |
# Time :2023/8/14 10:20 | |
# Author :小吕同学 | |
""" | |
import gradio as gr | |
import google.generativeai as genai | |
def request_to_v2(message, api_keys, context=[]): | |
context = [message] | |
# message = message.replace("\n","##") | |
try: | |
## 加入API_keys | |
genai.configure(api_key=api_keys) | |
model = genai.GenerativeModel(model_name="gemini-pro") | |
promt_s = [ | |
message | |
] | |
print(f"用户输入:{message}") | |
respose = model.generate_content(promt_s) | |
print(f"模型输出:{list(respose)}") | |
responses = respose.text | |
print(f"模型输入:{responses}") | |
# responses = "嘿嘿" | |
except Exception as e: | |
print(f"报错信息:{e}") | |
responses = f"!!!我出错啦!!!!\n报错内容为:{e}" | |
context += [responses] | |
responses = [(u, b) for u, b in zip(context[::2], context[1::2])] | |
return responses, context | |
with gr.Blocks() as dialog_app: | |
gr.HTML("""<h1 align="center">Claude2-API-xiaolv-第3版-单轮对话-支持文件知识库</h1>""") | |
with gr.Tab("Claude2 API xiaolv"): | |
gr.Markdown(""" | |
## 需要传入的参数有: | |
1.cookie:intercom-device-id-lupk8zyo=8888 | |
2.user_id:对应 organization_uuid | |
3.file:上传文件,可传可不传,如果需要基于文件进行问答,可以选择上传(支持:txt、csv、pdf格式) | |
## 特点: | |
1. 根据传入的cookie 和 user_id 自动创建一个新的聊天频道(conversation_uuid/channel_id); | |
2. 在新的聊天聊天频道进行对话; | |
3. 请求放回后,自动删除新的聊天频道,避免频繁调用导致页面太多记录。 | |
4. 模型只能进行一次聊天,对于特定场景比较有用,防止模型记住历史数据,影响模型对实际数据的输出额判断。 | |
""") | |
gr.HTML( | |
"""<p>5.如果想多轮对话的话,可以参考以下这个地址:<a href="https://huggingface.co/spaces/xiaolv/claude2_xiaolv">Claude2-API-xiaolv-第1版-多轮对话</a></p>""" | |
"""<p>6.如果只想回复一次的话,可以参考以下这个地址:<a href="https://huggingface.co/spaces/xiaolv/claude2_xiaolv_api_updata">Claude2-API-xiaolv-第2版-单轮对话</a></p>""" | |
"""<p>如果喜欢请给个 💖 </p>""" | |
) | |
with gr.Row(): | |
with gr.Column(scale=0.4): | |
api_keys = gr.Textbox(lines=2, label="输入GOOGLE_API_KEY") | |
with gr.Column(scale=0.6): | |
chatbot = gr.Chatbot([]) | |
state = gr.State([]) | |
with gr.Row(): | |
inputs = gr.Textbox( | |
label="输入问题", | |
placeholder="请输入你的文本,确保已经正确填入GOOGLE_API_KEY" | |
) | |
inputs.submit(request_to_v2, [inputs, api_keys, state], [chatbot, state]) | |
send = gr.Button("发送请求.....") | |
send.click(request_to_v2, [inputs, api_keys, state], [chatbot, state], | |
api_name="xiaolv_google_ai", show_progress=True) | |
gr.Markdown(""" | |
清除历史记录是,请输入:```/resrt```""") | |
# 为网站设置密码防止滥用 | |
# dialog_app.launch(auth=("admin", "2240560729"),show_error=True,show_api=True,share=True) | |
# dialog_app.launch(auth=("admin", "2240560729"),show_error=True) | |
dialog_app.launch(show_error=True) | |
# dialog_app.launch(show_error=True,show_api=True,share=True) |