Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +84 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# !/usr/bin/env python
|
| 2 |
+
# -*-coding:utf-8 -*-
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
# File : claude2_xiaolv_api_file_chat.py
|
| 6 |
+
# Time :2023/8/14 10:20
|
| 7 |
+
# Author :小吕同学
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import google.generativeai as genai
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def request_to_v2(message, api_keys, context=[]):
|
| 16 |
+
context = [message]
|
| 17 |
+
|
| 18 |
+
## 加入API_keys
|
| 19 |
+
genai.configure(api_key=api_keys)
|
| 20 |
+
|
| 21 |
+
model = genai.GenerativeModel(model_name="gemini-pro")
|
| 22 |
+
promt_s = [
|
| 23 |
+
message
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
respose = model.generate_content(promt_s)
|
| 27 |
+
responses = respose.text
|
| 28 |
+
|
| 29 |
+
# responses = "嘿嘿"
|
| 30 |
+
|
| 31 |
+
context += [responses]
|
| 32 |
+
responses = [(u, b) for u, b in zip(context[::2], context[1::2])]
|
| 33 |
+
return responses, context
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
with gr.Blocks() as dialog_app:
|
| 40 |
+
gr.HTML("""<h1 align="center">Claude2-API-xiaolv-第3版-单轮对话-支持文件知识库</h1>""")
|
| 41 |
+
with gr.Tab("Claude2 API xiaolv"):
|
| 42 |
+
gr.Markdown("""
|
| 43 |
+
## 需要传入的参数有:
|
| 44 |
+
1.cookie:intercom-device-id-lupk8zyo=8888
|
| 45 |
+
2.user_id:对应 organization_uuid
|
| 46 |
+
3.file:上传文件,可传可不传,如果需要基于文件进行问答,可以选择上传(支持:txt、csv、pdf格式)
|
| 47 |
+
## 特点:
|
| 48 |
+
1. 根据传入的cookie 和 user_id 自动创建一个新的聊天频道(conversation_uuid/channel_id);
|
| 49 |
+
2. 在新的聊天聊天频道进行对话;
|
| 50 |
+
3. 请求放回后,自动删除新的聊天频道,避免频繁调用导致页面太多记录。
|
| 51 |
+
4. 模型只能进行一次聊天,对于特定场景比较有用,防止模型记住历史数据,影响模型对实际数据的输出额判断。
|
| 52 |
+
""")
|
| 53 |
+
gr.HTML(
|
| 54 |
+
"""<p>5.如果想多轮对话的话,可以参考以下这个地址:<a href="https://huggingface.co/spaces/xiaolv/claude2_xiaolv">Claude2-API-xiaolv-第1版-多轮对话</a></p>"""
|
| 55 |
+
"""<p>6.如果只想回复一次的话,可以参考以下这个地址:<a href="https://huggingface.co/spaces/xiaolv/claude2_xiaolv_api_updata">Claude2-API-xiaolv-第2版-单轮对话</a></p>"""
|
| 56 |
+
"""<p>如果喜欢请给个 💖 </p>"""
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
with gr.Column(scale=0.4):
|
| 61 |
+
api_keys = gr.Textbox(lines=2, label="输入GOOGLE_API_KEY")
|
| 62 |
+
with gr.Column(scale=0.6):
|
| 63 |
+
chatbot = gr.Chatbot([])
|
| 64 |
+
|
| 65 |
+
state = gr.State([])
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
inputs = gr.Textbox(
|
| 69 |
+
label="输入问题",
|
| 70 |
+
placeholder="请输入你的文本,确保已经正确填入GOOGLE_API_KEY"
|
| 71 |
+
)
|
| 72 |
+
inputs.submit(request_to_v2, [inputs, api_keys, state], [chatbot, state])
|
| 73 |
+
send = gr.Button("发送请求.....")
|
| 74 |
+
send.click(request_to_v2, [inputs, api_keys, state], [chatbot, state],
|
| 75 |
+
api_name="xiaolv_google_ai", show_progress=True)
|
| 76 |
+
|
| 77 |
+
gr.Markdown("""
|
| 78 |
+
清除历史记录是,请输入:```/resrt```""")
|
| 79 |
+
|
| 80 |
+
# 为网站设置密码防止滥用
|
| 81 |
+
# dialog_app.launch(auth=("admin", "2240560729"),show_error=True,show_api=True,share=True)
|
| 82 |
+
# dialog_app.launch(auth=("admin", "2240560729"),show_error=True)
|
| 83 |
+
dialog_app.launch(show_error=True)
|
| 84 |
+
# dialog_app.launch(show_error=True,show_api=True,share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==3.23.0
|
| 2 |
+
protobuf==3.20.0
|
| 3 |
+
google-generativeai
|
| 4 |
+
|
| 5 |
+
|