siyuwang541 commited on
Commit
d63d9ed
·
verified ·
1 Parent(s): ee36856

track user ip as user_id

Browse files
Files changed (1) hide show
  1. app.py +35 -12
app.py CHANGED
@@ -3,8 +3,23 @@ from huggingface_hub import InferenceClient
3
 
4
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
 
6
- def process(audio, image):
 
 
 
 
 
 
 
 
 
 
 
 
7
  """处理语音和图片的示例函数"""
 
 
 
8
  if audio is not None:
9
  sample_rate, audio_data = audio
10
  audio_info = f"音频采样率: {sample_rate}Hz, 数据长度: {len(audio_data)}"
@@ -26,19 +41,25 @@ def respond(
26
  temperature,
27
  top_p,
28
  audio,
29
- image
 
30
  ):
 
 
 
 
31
  # 如果有上传的音频或图片,添加到消息中
32
  if audio is not None:
33
- # 这里可以添加音频处理逻辑
34
  audio_sample_rate, audio_data = audio
35
  message += f"\n[附加音频信息: 采样率 {audio_sample_rate}Hz, 时长 {len(audio_data)/audio_sample_rate:.2f}秒]"
36
 
37
  if image is not None:
38
- # 这里可以添加图片处理逻辑
39
  message += f"\n[附加图片信息: 尺寸 {image.shape}]"
40
 
41
- messages = [{"role": "system", "content": system_message}]
 
 
 
42
 
43
  for val in history:
44
  if val[0]:
@@ -92,7 +113,7 @@ with gr.Blocks() as app:
92
  def user(user_message, chat_history):
93
  return "", chat_history + [[user_message, None]]
94
 
95
- def bot(chat_history, system_message, max_tokens, temperature, top_p, audio, image):
96
  # 获取最后一条用户消息
97
  user_message = chat_history[-1][0]
98
 
@@ -106,19 +127,20 @@ with gr.Blocks() as app:
106
  temperature,
107
  top_p,
108
  audio,
109
- image
 
110
  ):
111
  bot_response = response
112
  chat_history[-1][1] = bot_response
113
  yield chat_history
114
 
115
- # 连接事件
116
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
117
- bot, [chatbot, system_msg, max_tokens, temperature, top_p, audio_input, image_input], chatbot
118
  )
119
 
120
  submit_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
121
- bot, [chatbot, system_msg, max_tokens, temperature, top_p, audio_input, image_input], chatbot
122
  )
123
 
124
  clear.click(lambda: None, None, chatbot, queue=False)
@@ -131,11 +153,12 @@ with gr.Blocks() as app:
131
  audio_output = gr.Textbox(label="音频信息")
132
  image_output = gr.Textbox(label="图片信息")
133
 
 
134
  process_btn.click(
135
  process,
136
- inputs=[audio_processor, image_processor],
137
  outputs=[audio_output, image_output]
138
  )
139
 
140
  if __name__ == "__main__":
141
- app.launch()
 
3
 
4
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
 
6
+ def get_client_ip(request: gr.Request):
7
+ """获取客户端真实IP地址"""
8
+ if request:
9
+ # 从请求头中获取真实IP(考虑代理情况)
10
+ x_forwarded_for = request.headers.get("x-forwarded-for", "")
11
+ if x_forwarded_for:
12
+ client_ip = x_forwarded_for.split(",")[0]
13
+ else:
14
+ client_ip = request.client.host
15
+ return client_ip
16
+ return "unknown"
17
+
18
+ def process(audio, image, request: gr.Request):
19
  """处理语音和图片的示例函数"""
20
+ client_ip = get_client_ip(request)
21
+ print(f"Processing request from IP: {client_ip}")
22
+
23
  if audio is not None:
24
  sample_rate, audio_data = audio
25
  audio_info = f"音频采样率: {sample_rate}Hz, 数据长度: {len(audio_data)}"
 
41
  temperature,
42
  top_p,
43
  audio,
44
+ image,
45
+ request: gr.Request # 添加请求对象
46
  ):
47
+ # 获取客户端IP
48
+ client_ip = get_client_ip(request)
49
+ print(f"Chat request from IP: {client_ip}")
50
+
51
  # 如果有上传的音频或图片,添加到消息中
52
  if audio is not None:
 
53
  audio_sample_rate, audio_data = audio
54
  message += f"\n[附加音频信息: 采样率 {audio_sample_rate}Hz, 时长 {len(audio_data)/audio_sample_rate:.2f}秒]"
55
 
56
  if image is not None:
 
57
  message += f"\n[附加图片信息: 尺寸 {image.shape}]"
58
 
59
+ # 可选:将IP信息添加到系统消息中
60
+ annotated_system_message = f"{system_message}\n[用户IP: {client_ip}]"
61
+
62
+ messages = [{"role": "system", "content": annotated_system_message}]
63
 
64
  for val in history:
65
  if val[0]:
 
113
  def user(user_message, chat_history):
114
  return "", chat_history + [[user_message, None]]
115
 
116
+ def bot(chat_history, system_message, max_tokens, temperature, top_p, audio, image, request: gr.Request):
117
  # 获取最后一条用户消息
118
  user_message = chat_history[-1][0]
119
 
 
127
  temperature,
128
  top_p,
129
  audio,
130
+ image,
131
+ request # 传递请求对象
132
  ):
133
  bot_response = response
134
  chat_history[-1][1] = bot_response
135
  yield chat_history
136
 
137
+ # 连接事件 - 添加request参数
138
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
139
+ bot, [chatbot, system_msg, max_tokens, temperature, top_p, audio_input, image_input, gr.Request()], chatbot
140
  )
141
 
142
  submit_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
143
+ bot, [chatbot, system_msg, max_tokens, temperature, top_p, audio_input, image_input, gr.Request()], chatbot
144
  )
145
 
146
  clear.click(lambda: None, None, chatbot, queue=False)
 
153
  audio_output = gr.Textbox(label="音频信息")
154
  image_output = gr.Textbox(label="图片信息")
155
 
156
+ # 添加request参数到处理函数
157
  process_btn.click(
158
  process,
159
+ inputs=[audio_processor, image_processor, gr.Request()],
160
  outputs=[audio_output, image_output]
161
  )
162
 
163
  if __name__ == "__main__":
164
+ app.launch()