qiuhuachuan commited on
Commit
9e385c8
1 Parent(s): 3e93681

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +7 -58
README.md CHANGED
@@ -22,17 +22,11 @@ tags:
22
  ## Code
23
  ```
24
  import os
25
- import ujson
26
- from typing import Optional
27
 
28
- os.environ['CUDA_VISIBLE_DEVICES'] = '2'
29
 
30
- import uvicorn
31
  import torch
32
  from transformers import AutoTokenizer, AutoModel
33
- from fastapi import FastAPI
34
- from fastapi.middleware.cors import CORSMiddleware
35
- from pydantic import BaseModel
36
 
37
  from peft import PeftModel
38
 
@@ -47,27 +41,6 @@ tokenizer = AutoTokenizer.from_pretrained('THUDM/chatglm-6b',
47
  trust_remote_code=True)
48
 
49
 
50
- class ChatInfo(BaseModel):
51
- owner: str
52
- msg: str
53
- unique_id: str
54
-
55
-
56
- class RatingInfo(BaseModel):
57
- thumb_up: Optional[bool]
58
- thumb_down: Optional[bool]
59
- unique_id: str
60
- idx: int
61
-
62
-
63
- app = FastAPI()
64
- app.add_middleware(CORSMiddleware,
65
- allow_origins=['*'],
66
- allow_credentials=True,
67
- allow_methods=['*'],
68
- allow_headers=['*'])
69
-
70
-
71
  def format_example(example: dict) -> dict:
72
  context = f'''Input: {example['input']}\n'''
73
 
@@ -88,30 +61,16 @@ def generate_response(data: dict):
88
  temperature=0.9,
89
  top_p=0.9)
90
 
91
- raw_out_text = tokenizer.decode(out[0])
92
  true_out_text = tokenizer.decode(out[0][input_length:])
93
 
94
  answer = true_out_text.replace('\nEND', '').strip()
95
  return answer
96
 
97
 
98
- @app.post('/v1/chat')
99
- async def chat(ChatInfo: ChatInfo):
100
- unique_id = ChatInfo.unique_id
101
- # './dialogues'用于存储聊天数据
102
- existing_files = os.listdir('./dialogues')
103
-
104
- target_file = f'{unique_id}.json'
105
- if target_file in existing_files:
106
- with open(f'./dialogues/{unique_id}.json', 'r', encoding='utf-8') as f:
107
- data: list = ujson.load(f)
108
- else:
109
- data = []
110
- data.append({
111
- 'owner': ChatInfo.owner,
112
- 'msg': ChatInfo.msg,
113
- 'unique_id': ChatInfo.unique_id
114
- })
115
  input_str = ''
116
  for item in data:
117
  if item['owner'] == 'seeker':
@@ -129,19 +88,9 @@ async def chat(ChatInfo: ChatInfo):
129
  wrapped_data = {'input': input_str}
130
 
131
  response = generate_response(data=wrapped_data)
132
- supporter_msg = {
133
- 'owner': 'supporter',
134
- 'msg': response,
135
- 'unique_id': unique_id
136
- }
137
  data.append(supporter_msg)
138
- with open(f'./dialogues/{unique_id}.json', 'w', encoding='utf-8') as f:
139
- ujson.dump(data, f, ensure_ascii=False, indent=2)
140
- return {'item': supporter_msg, 'responseCode': 200}
141
-
142
-
143
- if __name__ == '__main__':
144
- uvicorn.run(app, host='0.0.0.0', port=8000)
145
  ```
146
 
147
 
 
22
  ## Code
23
  ```
24
  import os
 
 
25
 
26
+ os.environ['CUDA_VISIBLE_DEVICES'] = '6'
27
 
 
28
  import torch
29
  from transformers import AutoTokenizer, AutoModel
 
 
 
30
 
31
  from peft import PeftModel
32
 
 
41
  trust_remote_code=True)
42
 
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def format_example(example: dict) -> dict:
45
  context = f'''Input: {example['input']}\n'''
46
 
 
61
  temperature=0.9,
62
  top_p=0.9)
63
 
 
64
  true_out_text = tokenizer.decode(out[0][input_length:])
65
 
66
  answer = true_out_text.replace('\nEND', '').strip()
67
  return answer
68
 
69
 
70
+ data = []
71
+ while True:
72
+ seeker_msg = input('求助者:')
73
+ data.append({'owner': 'seeker', 'msg': seeker_msg})
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  input_str = ''
75
  for item in data:
76
  if item['owner'] == 'seeker':
 
88
  wrapped_data = {'input': input_str}
89
 
90
  response = generate_response(data=wrapped_data)
91
+ print(f'支持者:{response}')
92
+ supporter_msg = {'owner': 'supporter', 'msg': response}
 
 
 
93
  data.append(supporter_msg)
 
 
 
 
 
 
 
94
  ```
95
 
96