Commit ·
efa3ae6
1
Parent(s): afa1066
Remove cancel generation feature as it didn't work
Browse files17 unmodified lines181920212223242526272829303132333435379 unmodified lines41541641741841942042184 unmodified lines5065075085095105115126 unmodified lines51952052152252352452552618 unmodified lines5455465475485495505515529 unmodified lines5625635645655665675685692 unmodified lines5725735745755765775785795 unmodified lines58558658758858959059159259359459546 unmodified lines64264364464564664764821 unmodified lines67067167267367467567617 unmodified lines# Optional: Disable GPU visibility if you wish to force CPU usage# os.environ["CUDA_VISIBLE_DEVICES"] = ""# ------------------------------# Global Cancellation Event# ------------------------------cancel_event = threading.Event()# ------------------------------# Stopping Criteria for Cancellation# ------------------------------class CancelStoppingCriteria(StoppingCriteria):def __call__(self, input_ids, scores, **kwargs):return cancel_event.is_set()# ------------------------------# Torch-Compatible Model Definitions with Adjusted Descriptions# ------------------------------379 unmodified lines"""Generates streaming chat responses, optionally with background web search."""cancel_event.clear()history = list(chat_history or [])history.append({'role': 'user', 'content': user_msg})84 unmodified lines'top_p': top_p,'repetition_penalty': repeat_penalty,'streamer': streamer,'stopping_criteria': [CancelStoppingCriteria()],'return_full_text': False,})6 unmodified lines# Stream tokensfor chunk in streamer:if cancel_event.is_set():breaktext = chunk# Detect start of thinking18 unmodified lineshistory.append({'role': 'assistant', 'content': answer_buf})else:history[-1]['content'] = thought_bufif cancel_event.is_set():breakyield history, debugcontinue9 unmodified lineshistory.append({'role': 'assistant', 'content': answer_buf})else:history[-1]['content'] = thought_bufif cancel_event.is_set():breakyield history, debugcontinue2 unmodified lineshistory.append({'role': 'assistant', 'content': ''})answer_buf += texthistory[-1]['content'] = answer_bufif cancel_event.is_set():breakyield history, debuggen_thread.join()5 unmodified linesgc.collect()def cancel_generation():cancel_event.set()return 'Generation cancelled.'def update_default_prompt(enable_search):return f"You are a helpful assistant."46 unmodified linesmc = gr.Number(value=50, precision=0, label="Max Chars/Result")st = gr.Slider(minimum=0.0, maximum=30.0, step=0.5, value=5.0, label="Search Timeout (s)")clr = gr.Button("Clear Chat")cnl = gr.Button("Cancel Generation")with gr.Column(scale=7):chat = gr.Chatbot(type="messages")txt = gr.Textbox(placeholder="Type your message and press Enter...")21 unmodified linessearch_chk.change(fn=update_default_prompt, inputs=search_chk, outputs=sys_prompt)clr.click(fn=lambda: ([], "", ""), outputs=[chat, txt, dbg])cnl.click(fn=cancel_generation, outputs=dbg)txt.submit(fn=chat_response,inputs=[txt, chat, sys_prompt, search_chk, mr, mc,model_dd, max_tok, temp, k, p, rp, st],17 unmodified lines181920212223379 unmodified lines40340440540640740884 unmodified lines4934944954964974986 unmodified lines50550650750850951018 unmodified lines5295305315325335349 unmodified lines5445455465475485492 unmodified lines5525535545555565575 unmodified lines56356456556656756846 unmodified lines61561661761861962021 unmodified lines64264364464564664717 unmodified lines# Optional: Disable GPU visibility if you wish to force CPU usage# os.environ["CUDA_VISIBLE_DEVICES"] = ""# ------------------------------# Torch-Compatible Model Definitions with Adjusted Descriptions# ------------------------------379 unmodified lines"""Generates streaming chat responses, optionally with background web search."""history = list(chat_history or [])history.append({'role': 'user', 'content': user_msg})84 unmodified lines'top_p': top_p,'repetition_penalty': repeat_penalty,'streamer': streamer,'return_full_text': False,})6 unmodified lines# Stream tokensfor chunk in streamer:text = chunk# Detect start of thinking18 unmodified lineshistory.append({'role': 'assistant', 'content': answer_buf})else:history[-1]['content'] = thought_bufyield history, debugcontinue9 unmodified lineshistory.append({'role': 'assistant', 'content': answer_buf})else:history[-1]['content'] = thought_bufyield history, debugcontinue2 unmodified lineshistory.append({'role': 'assistant', 'content': ''})answer_buf += texthistory[-1]['content'] = answer_bufyield history, debuggen_thread.join()5 unmodified linesgc.collect()def update_default_prompt(enable_search):return f"You are a helpful assistant."46 unmodified linesmc = gr.Number(value=50, precision=0, label="Max Chars/Result")st = gr.Slider(minimum=0.0, maximum=30.0, step=0.5, value=5.0, label="Search Timeout (s)")clr = gr.Button("Clear Chat")with gr.Column(scale=7):chat = gr.Chatbot(type="messages")txt = gr.Textbox(placeholder="Type your message and press Enter...")21 unmodified linessearch_chk.change(fn=update_default_prompt, inputs=search_chk, outputs=sys_prompt)clr.click(fn=lambda: ([], "", ""), outputs=[chat, txt, dbg])txt.submit(fn=chat_response,inputs=[txt, chat, sys_prompt, search_chk, mr, mc,model_dd, max_tok, temp, k, p, rp, st],
app.py-29