xiaolv commited on
Commit
059b687
1 Parent(s): eb66930

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -4
app.py CHANGED
@@ -3,7 +3,7 @@ import gradio as gr
3
  import uuid
4
  import time
5
  import random
6
-
7
 
8
  # 创建新的聊天频道
9
  def generate_uuid():
@@ -67,7 +67,75 @@ def create_new_chat(user_id,cookie):
67
  # Returns JSON of the newly created conversation information
68
  return response.json()
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  def request_to_v2(message, cookie, user_id,context=[]):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  response_json = create_new_chat(user_id,cookie)
73
  channel_id = response_json.get('uuid',"")
@@ -108,7 +176,7 @@ def request_to_v2(message, cookie, user_id,context=[]):
108
  "organization_uuid": user_id,
109
  "conversation_uuid": channel_id,
110
  "text": message,
111
- "attachments": []
112
  }
113
  post_msg_data = json.dumps(post_msg_data)
114
  # print(post_msg_data)
@@ -182,6 +250,7 @@ with gr.Blocks() as dialog_app:
182
  with gr.Column(scale=0.4):
183
  cookies = gr.Textbox(lines=2, label="输入cookies")
184
  user_id = gr.Textbox(lines=2, label="输入user_id/organization_uuid")
 
185
  # channel_id = gr.Textbox(lines=2, label="输入channel_id/conversation_uuid")
186
  # chatbot = gr.Chatbot()
187
  with gr.Column(scale=0.6):
@@ -194,9 +263,9 @@ with gr.Blocks() as dialog_app:
194
  label="输入问题",
195
  placeholder="请输入你的文本,确保已经正确填入cookies、user_id"
196
  )
197
- inputs.submit(request_to_v2, [inputs, cookies, user_id,state], [chatbot, state])
198
  send = gr.Button("发送请求.....")
199
- send.click(request_to_v2, [inputs, cookies, user_id,state], [chatbot, state], api_name="xiaolvgpt", show_progress=True)
200
 
201
 
202
  gr.Markdown("""
 
3
  import uuid
4
  import time
5
  import random
6
+ import os
7
 
8
  # 创建新的聊天频道
9
  def generate_uuid():
 
67
  # Returns JSON of the newly created conversation information
68
  return response.json()
69
 
70
+ def get_content_type(file_path):
71
+ # Function to determine content type based on file extension
72
+ extension = os.path.splitext(file_path)[-1].lower()
73
+ if extension == '.pdf':
74
+ return 'application/pdf'
75
+ elif extension == '.txt':
76
+ return 'text/plain'
77
+ elif extension == '.csv':
78
+ return 'text/csv'
79
+ # Add more content types as needed for other file types
80
+ else:
81
+ return 'application/octet-stream'
82
+
83
+ def upload_attachment(cookie ,organization_id,file_path):
84
+ url = 'https://claude.ai/api/convert_document'
85
+ headers = {
86
+ 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82',
87
+ 'Accept-Language': 'en-US,en;q=0.5',
88
+ 'Referer': 'https://claude.ai/chats',
89
+ 'Origin': 'https://claude.ai',
90
+ 'Sec-Fetch-Dest': 'empty',
91
+ 'Sec-Fetch-Mode': 'cors',
92
+ 'Sec-Fetch-Site': 'same-origin',
93
+ 'Connection': 'keep-alive',
94
+ 'Cookie': f'{cookie}',
95
+ 'TE': 'trailers'
96
+ }
97
+
98
+ file_name = os.path.basename(file_path)
99
+ print(file_name)
100
+ content_type = get_content_type(file_path)
101
+ print(content_type)
102
+ files = {
103
+ 'file': (file_name, open(file_path, 'rb'), content_type),
104
+ 'orgUuid': (None, organization_id)
105
+ }
106
+ print(files)
107
+ response = requests.post(url, headers=headers, files=files)
108
+ # 使用代理,解决国内网络不能访问问题
109
+ #response = request(url, headers=headers, files=files,proxies=proxies)
110
+ if response.status_code == 200:
111
+ return response.json()
112
+ else:
113
+ return False
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
  def request_to_v2(message, cookie, user_id,context=[]):
123
+ ## 是否加入文件
124
+ # Upload attachment if provided
125
+ attachment = attachment.name
126
+ attachments = []
127
+ if attachment:
128
+ attachment_response = upload_attachment(cookie,user_id,attachment)
129
+ if attachment_response:
130
+ attachments = [attachment_response]
131
+ else:
132
+ return {"Error: Invalid file format. Please try again."}
133
+
134
+ # Ensure attachments is an empty list when no attachment is provided
135
+ if not attachment:
136
+ attachments = []
137
+
138
+
139
 
140
  response_json = create_new_chat(user_id,cookie)
141
  channel_id = response_json.get('uuid',"")
 
176
  "organization_uuid": user_id,
177
  "conversation_uuid": channel_id,
178
  "text": message,
179
+ "attachments": attachments
180
  }
181
  post_msg_data = json.dumps(post_msg_data)
182
  # print(post_msg_data)
 
250
  with gr.Column(scale=0.4):
251
  cookies = gr.Textbox(lines=2, label="输入cookies")
252
  user_id = gr.Textbox(lines=2, label="输入user_id/organization_uuid")
253
+ file = gr.File(label='请上传知识库文件, 目前支持txt、docx、md格式',file_types=['.txt', '.csv', '.pdf'])
254
  # channel_id = gr.Textbox(lines=2, label="输入channel_id/conversation_uuid")
255
  # chatbot = gr.Chatbot()
256
  with gr.Column(scale=0.6):
 
263
  label="输入问题",
264
  placeholder="请输入你的文本,确保已经正确填入cookies、user_id"
265
  )
266
+ inputs.submit(request_to_v2, [inputs, cookies, user_id,file,state], [chatbot, state])
267
  send = gr.Button("发送请求.....")
268
+ send.click(request_to_v2, [inputs, cookies, user_id,file,state], [chatbot, state], api_name="xiaolvgpt", show_progress=True)
269
 
270
 
271
  gr.Markdown("""