CMLL commited on
Commit
ad01e97
1 Parent(s): 97ec7d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -40
app.py CHANGED
@@ -48,36 +48,7 @@ def generate(
48
  top_k: int = 50,
49
  repetition_penalty: float = 1.2,
50
  ) -> Iterator[str]:
51
- conversation = [{"role": "system", "content": system_prompt}]
52
- for user, assistant in chat_history:
53
- conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
54
- conversation.append({"role": "user", "content": message})
55
-
56
- input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
57
- if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
58
- input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
59
- gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
60
- input_ids = input_ids.to(model.device)
61
-
62
- streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
63
- generate_kwargs = dict(
64
- {"input_ids": input_ids},
65
- streamer=streamer,
66
- max_new_tokens=max_new_tokens,
67
- do_sample=True,
68
- top_p=top_p,
69
- top_k=top_k,
70
- temperature=temperature,
71
- num_beams=1,
72
- repetition_penalty=repetition_penalty,
73
- )
74
- t = Thread(target=model.generate, kwargs=generate_kwargs)
75
- t.start()
76
-
77
- outputs = []
78
- for text in streamer:
79
- outputs.append(text)
80
- yield "".join(outputs)
81
 
82
  chat_interface = gr.ChatInterface(
83
  fn=generate,
@@ -128,18 +99,86 @@ chat_interface = gr.ChatInterface(
128
  ["写一篇关于'AI在中医研究中的应用'的100字文章。"],
129
  ["写一篇从中医角度关于'秋季女性健康调养方案'的1000字科普文章,从季节变化、饮食调理、活动养生等方面进行阐述"],
130
  ],
131
- chatbot=gr.Chatbot(height=600), # 增加聊天框高度
132
  )
133
 
134
- with gr.Blocks(css="style.css") as demo:
135
- with gr.Row():
136
- with gr.Column(scale=2):
137
- gr.Markdown(DESCRIPTION)
138
- gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
139
- chat_interface.render()
140
- with gr.Column(scale=1):
141
- gr.Markdown("## Additional Information")
142
- gr.Markdown(LICENSE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
  if __name__ == "__main__":
145
  demo.queue(max_size=20).launch()
 
48
  top_k: int = 50,
49
  repetition_penalty: float = 1.2,
50
  ) -> Iterator[str]:
51
+ # ... (保持 generate 函数不变)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  chat_interface = gr.ChatInterface(
54
  fn=generate,
 
99
  ["写一篇关于'AI在中医研究中的应用'的100字文章。"],
100
  ["写一篇从中医角度关于'秋季女性健康调养方案'的1000字科普文章,从季节变化、饮食调理、活动养生等方面进行阐述"],
101
  ],
 
102
  )
103
 
104
+ css = """
105
+ body, .gradio-container {
106
+ margin: 0;
107
+ padding: 0;
108
+ height: 100vh;
109
+ overflow: hidden;
110
+ }
111
+ #main-container {
112
+ display: flex;
113
+ height: 100vh;
114
+ flex-direction: column;
115
+ }
116
+ #description {
117
+ padding: 10px;
118
+ overflow-y: auto;
119
+ flex-shrink: 0;
120
+ }
121
+ #chat-container {
122
+ display: flex;
123
+ flex-grow: 1;
124
+ overflow: hidden;
125
+ }
126
+ #chat-column {
127
+ flex: 2;
128
+ display: flex;
129
+ flex-direction: column;
130
+ overflow: hidden;
131
+ }
132
+ #info-column {
133
+ flex: 1;
134
+ overflow-y: auto;
135
+ padding: 10px;
136
+ }
137
+ #chatbot {
138
+ flex-grow: 1;
139
+ overflow-y: auto;
140
+ }
141
+ """
142
+
143
+ js = """
144
+ function adjustHeight() {
145
+ const mainContainer = document.getElementById('main-container');
146
+ const description = document.getElementById('description');
147
+ const chatContainer = document.getElementById('chat-container');
148
+ const chatbot = document.getElementById('chatbot');
149
+
150
+ const windowHeight = window.innerHeight;
151
+ const descriptionHeight = description.offsetHeight;
152
+ const availableHeight = windowHeight - descriptionHeight;
153
+
154
+ chatContainer.style.height = `${availableHeight}px`;
155
+ chatbot.style.height = `${availableHeight * 0.7}px`;
156
+ }
157
+
158
+ // 在页面加载完成后调整高度
159
+ window.addEventListener('load', adjustHeight);
160
+ // 在窗口大小改变时调整高度
161
+ window.addEventListener('resize', adjustHeight);
162
+ """
163
+
164
+ with gr.Blocks(css=css) as demo:
165
+ gr.HTML(f"""
166
+ <div id="main-container">
167
+ <div id="description">{DESCRIPTION}</div>
168
+ <div id="chat-container">
169
+ <div id="chat-column">
170
+ <div id="chatbot"></div>
171
+ </div>
172
+ <div id="info-column">
173
+ <h2>Additional Information</h2>
174
+ {LICENSE}
175
+ </div>
176
+ </div>
177
+ </div>
178
+ """)
179
+ gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
180
+ chat_interface.render()
181
+ gr.HTML("<script>" + js + "</script>")
182
 
183
  if __name__ == "__main__":
184
  demo.queue(max_size=20).launch()