Funpee commited on
Commit
800de3d
·
1 Parent(s): be580b4

首次提交

Browse files
Files changed (3) hide show
  1. README.md +5 -8
  2. app.py +83 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,13 +1,10 @@
1
  ---
2
- title: MiAi Promt Assistant
3
- emoji: 💻
4
- colorFrom: purple
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.4.0
8
  app_file: app.py
9
  pinned: false
10
- short_description: miai-pa
11
  ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: 米你AI 提示词助手
3
+ emoji: 🤖
4
+ colorFrom: blue
5
+ colorTo: pink
6
  sdk: gradio
7
+ sdk_version: 4.19.2
8
  app_file: app.py
9
  pinned: false
 
10
  ---
 
 
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ import sseclient
5
+ import sys
6
+ import os
7
+ import spaces
8
+ import torch
9
+
10
+ COZE_API_URL = "https://api.coze.cn/v3/chat"
11
+ API_KEY = os.environ.get("COZE_API_KEY", "your_default_key")
12
+ BOT_ID = os.environ.get("COZE_BOT_ID", "your_default_bot_id")
13
+
14
+ # 初始化 GPU tensor
15
+ zero = torch.Tensor([0]).cuda()
16
+
17
+ @spaces.GPU
18
+ def chat_stream(message, history):
19
+ # 验证 GPU 是否可用
20
+ print(f"当前设备: {zero.device}", file=sys.stderr)
21
+
22
+ headers = {
23
+ "Authorization": f"Bearer {API_KEY}",
24
+ "Content-Type": "application/json"
25
+ }
26
+
27
+ # 构建包含历史记录的消息列表
28
+ messages = []
29
+ for human, assistant in history:
30
+ messages.append({"role": "user", "content": human, "content_type": "text"})
31
+ messages.append({"role": "assistant", "content": assistant, "content_type": "text"})
32
+ messages.append({"role": "user", "content": message, "content_type": "text"})
33
+
34
+ data = {
35
+ "bot_id": BOT_ID,
36
+ "user_id": "123123***", # 可以根据需要修改用户ID
37
+ "stream": True,
38
+ "auto_save_history": True,
39
+ "additional_messages": messages
40
+ }
41
+
42
+ print(f"发送到API的数据: {json.dumps(data, ensure_ascii=False)}", file=sys.stderr)
43
+
44
+ response = requests.post(COZE_API_URL, headers=headers, data=json.dumps(data), stream=True)
45
+ client = sseclient.SSEClient(response)
46
+
47
+ full_response = ""
48
+ for event in client.events():
49
+ if event.data == "[DONE]":
50
+ break
51
+ try:
52
+ chunk = json.loads(event.data)
53
+ if isinstance(chunk, dict) and "content" in chunk and chunk.get("type") == "answer":
54
+ full_response = chunk["content"]
55
+ except json.JSONDecodeError:
56
+ pass
57
+
58
+ print(f"完整的响应: {full_response}", file=sys.stderr)
59
+ return full_response
60
+
61
+ iface = gr.ChatInterface(
62
+ chat_stream,
63
+ chatbot=gr.Chatbot(height=400, label="提示词助手"),
64
+ textbox=gr.Textbox(placeholder="在这里输入您的消息...", container=False, scale=7),
65
+ title="米你AI 提示词助手",
66
+ description="帮助你生成提示词,输入一个主题,我会帮你联想合适的提示词",
67
+ theme="soft",
68
+ examples=[
69
+ "一个站在树上的小猴子",
70
+ "今天天气怎么样?",
71
+ "给我讲个笑话"
72
+ ],
73
+ cache_examples=False,
74
+ retry_btn="重试",
75
+ undo_btn="撤销",
76
+ clear_btn="清除",
77
+ submit_btn="发送",
78
+ stop_btn="停止",
79
+ autofocus=True
80
+ )
81
+
82
+ if __name__ == "__main__":
83
+ iface.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ requests
3
+ sseclient-py