update glm to use hf inference with novita
Browse files412 unmodified lines4134144154164174184191121 unmodified lines1541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176476 unmodified lines18411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371 unmodified line19391940194119421943194419454 unmodified lines19501951195219531954195519561 unmodified line19581959196019611962196319645 unmodified lines19701971197219731974197519761 unmodified line1978197919801981198219831984412 unmodified lines},{"name": "GLM-4.5","id": "GLM-4.5","description": "GLM-4.5 model with thinking capabilities for advanced code generation"},{1121 unmodified linesreturn f"Error extracting website content: {str(e)}"# GLM-4.5 Model Implementationstop_generation = Falsedef stream_from_vllm(messages, thinking_enabled=True, temperature=1.0):global stop_generation# Get GLM API configuration from environment variablesglm_api_key = os.getenv('OPENAI_API_KEY')glm_base_url = os.getenv('GLM_BASE_URL', 'https://open.bigmodel.cn/api/paas/v4/')if not glm_api_key:# Return configuration error if no API keyerror_msg = """GLM-4.5 API Key Not ConfiguredTo use GLM-4.5, please:1. Get your API key from: https://open.bigmodel.cn/2. Set environment variable: OPENAI_API_KEY=your_api_key_here3. Optionally set GLM_BASE_URL if using different endpointExample HTML code generation with Gradio:```html<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Sample App</title><style>body { font-family: Arial, sans-serif; margin: 40px; }.container { max-width: 600px; margin: 0 auto; }h1 { color: #333; }.button {background: #007acc;color: white;padding: 10px 20px;border: none;border-radius: 5px;cursor: pointer;}</style></head><body><div class="container"><h1>GLM-4.5 Configuration Required</h1><p>Please configure your GLM-4.5 API key to use this model.</p><button class="button" onclick="alert('Configure OPENAI_API_KEY environment variable')">Get Started</button></div></body></html>```"""yield type('Delta', (), {'content': error_msg, 'reasoning_content': None})()return# Configure OpenAI client for GLM-4.5try:client = OpenAI(base_url=glm_base_url,api_key=glm_api_key,)response = client.chat.completions.create(model="GLM-4.5",messages=messages,temperature=temperature,stream=True,max_tokens=65536,extra_body={"thinking": {"type": "enabled" if thinking_enabled else "disabled",}})for chunk in response:if stop_generation:breakif chunk.choices and chunk.choices[0].delta:yield chunk.choices[0].deltaexcept Exception as e:# Fallback: if GLM-4.5 API fails, yield error with sample codeerror_msg = f"""Error connecting to GLM-4.5: {str(e)}Please check:1. OPENAI_API_KEY environment variable is set correctly2. API key is valid and has credits3. Network connection is working4. GLM_BASE_URL is correct (current: {glm_base_url})Here's a sample HTML code to test the UI:```html<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>GLM-4.5 Error - Sample Output</title><style>body {{font-family: Arial, sans-serif;margin: 40px;background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);color: white;}}.container {{max-width: 600px;margin: 0 auto;background: rgba(255,255,255,0.1);padding: 30px;border-radius: 15px;backdrop-filter: blur(10px);}}h1 {{ color: #fff; text-align: center; }}.error {{ background: rgba(255,0,0,0.2); padding: 15px; border-radius: 8px; margin: 20px 0; }}.button {{background: rgba(255,255,255,0.2);color: white;padding: 12px 24px;border: 1px solid rgba(255,255,255,0.3);border-radius: 8px;cursor: pointer;display: block;margin: 20px auto;}}.button:hover {{ background: rgba(255,255,255,0.3); }}</style></head><body><div class="container"><h1>🤖 GLM-4.5 Configuration Error</h1><div class="error"><strong>Error:</strong> {str(e)}</div><p>This is a sample HTML output to demonstrate the UI while you configure GLM-4.5.</p><button class="button" onclick="window.open('https://open.bigmodel.cn/', '_blank')">Configure GLM-4.5 API</button></div><script>console.log('GLM-4.5 API Error: {str(e)}');</script></body></html>```"""print(f"GLM-4.5 API Error: {e}")yield type('Delta', (), {'content': error_msg, 'reasoning_content': None})()class GLM45Model:def __init__(self):self.accumulated_content = ""self.accumulated_reasoning = ""def reset_state(self):self.accumulated_content = ""self.accumulated_reasoning = ""def _render_response(self, reasoning_content, regular_content, skip_think=False):html_parts = []if reasoning_content and not skip_think:reasoning_escaped = html.escape(reasoning_content).replace("\n", "<br>")think_html = ("<details open><summary style='cursor:pointer;font-weight:bold;color:#007acc;'>Thinking</summary>""<div style='color:#555555;line-height:1.6;padding:15px;border-left:4px solid #007acc;margin:10px 0;background-color:#f0f7ff;border-radius:4px;'>"+ reasoning_escaped +"</div></details>")html_parts.append(think_html)if regular_content:content_escaped = html.escape(regular_content).replace("\n", "<br>")content_html = f"<div style='margin:0.5em 0; white-space: pre-wrap; line-height:1.6;'>{content_escaped}</div>"html_parts.append(content_html)return "".join(html_parts)def _build_messages(self, raw_hist, sys_prompt):msgs = []if sys_prompt.strip():msgs.append({"role": "system", "content": sys_prompt.strip()})for h in raw_hist:if h["role"] == "user":msgs.append({"role": "user", "content": h["content"]})else:msg = {"role": "assistant", "content": h.get("content", "")}if h.get("reasoning_content"):msg["reasoning_content"] = h.get("reasoning_content")msgs.append(msg)return msgsdef stream_generate(self, raw_hist, sys_prompt, thinking_enabled=True, temperature=1.0):global stop_generationstop_generation = Falsemsgs = self._build_messages(raw_hist, sys_prompt)self.reset_state()try:for delta in stream_from_vllm(msgs, thinking_enabled, temperature):if stop_generation:breakif hasattr(delta, 'content') and delta.content:self.accumulated_content += delta.contentif hasattr(delta, 'reasoning_content') and delta.reasoning_content:self.accumulated_reasoning += delta.reasoning_contentyield self._render_response(self.accumulated_reasoning, self.accumulated_content, not thinking_enabled)except Exception as e:yield self._render_response("", f"Error: {str(e)}")# Global GLM-4.5 instanceglm45 = GLM45Model()def generation_code(query: Optional[str], image: Optional[gr.Image], file: Optional[str], website_url: Optional[str], _setting: Dict[str, str], _history: Optional[History], _current_model: Dict, enable_search: bool = False, language: str = "html", provider: str = "auto"):if query is None:76 unmodified lines# Enhance query with search if enabledenhanced_query = enhance_query_with_search(query, enable_search)# Check if this is GLM-4.5 model and handle differentlyif _current_model["id"] == "GLM-4.5":# For GLM-4.5, use the specialized implementation with simpler streamingif image is not None:messages.append(create_multimodal_message(enhanced_query, image))else:messages.append({'role': 'user', 'content': enhanced_query})content = ""reasoning_content = ""try:# Use GLM-4.5 streaming directlyfor delta in stream_from_vllm(messages, True, 1.0):if stop_generation:breakif hasattr(delta, 'content') and delta.content:content += delta.contentif hasattr(delta, 'reasoning_content') and delta.reasoning_content:reasoning_content += delta.reasoning_content# Show streaming content (extract just the code part)clean_code = remove_code_block(content)search_status = " (with web search)" if enable_search and tavily_client else ""# Handle different language outputs for GLM-4.5 during streamingif language == "transformers.js":files = parse_transformers_js_output(clean_code)if files['index.html'] and files['index.js'] and files['style.css']:formatted_output = format_transformers_js_output(files)yield {code_output: gr.update(value=formatted_output, language="html"),history_output: history_to_chatbot_messages(_history),sandbox: send_to_sandbox(files['index.html']) if files['index.html'] else "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your code using the download button above.</div>",}else:yield {code_output: gr.update(value=clean_code, language="html"),history_output: history_to_chatbot_messages(_history),sandbox: "<div style='padding:1em;color:#888;text-align:center;'>Generating transformers.js app...</div>",}elif language == "svelte":yield {code_output: gr.update(value=clean_code, language="html"),history_output: history_to_chatbot_messages(_history),sandbox: "<div style='padding:1em;color:#888;text-align:center;'>Generating Svelte app...</div>",}else:if has_existing_content:if clean_code.strip().startswith("<!DOCTYPE html>") or clean_code.strip().startswith("<html"):yield {code_output: gr.update(value=clean_code, language=get_gradio_language(language)),history_output: history_to_chatbot_messages(_history),sandbox: send_to_sandbox(clean_code) if language == "html" else "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your code using the download button above.</div>",}else:last_content = _history[-1][1] if _history and len(_history[-1]) > 1 else ""modified_content = apply_search_replace_changes(last_content, clean_code)clean_content = remove_code_block(modified_content)yield {code_output: gr.update(value=clean_content, language=get_gradio_language(language)),history_output: history_to_chatbot_messages(_history),sandbox: send_to_sandbox(clean_content) if language == "html" else "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your code using the download button above.</div>",}else:yield {code_output: gr.update(value=clean_code, language=get_gradio_language(language)),history_output: history_to_chatbot_messages(_history),sandbox: send_to_sandbox(clean_code) if language == "html" else "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your code using the download button above.</div>",}except Exception as e:content = f"Error: {str(e)}"print(f"GLM-4.5 Error: {e}")# Final processing for GLM-4.5clean_code = remove_code_block(content)# Store content with thinking information if availableif reasoning_content:full_response = f"**Thinking:**\n{reasoning_content}\n\n**Code:**\n{content}"else:full_response = contentif language == "transformers.js":files = parse_transformers_js_output(clean_code)if files['index.html'] and files['index.js'] and files['style.css']:formatted_output = format_transformers_js_output(files)_history.append([query, full_response])yield {code_output: formatted_output,history: _history,1 unmodified linehistory_output: history_to_chatbot_messages(_history),}else:_history.append([query, full_response])yield {code_output: clean_code,history: _history,4 unmodified linesfiles = parse_svelte_output(clean_code)if files['src/App.svelte'] and files['src/app.css']:formatted_output = format_svelte_output(files)_history.append([query, full_response])yield {code_output: formatted_output,history: _history,1 unmodified linehistory_output: history_to_chatbot_messages(_history),}else:_history.append([query, full_response])yield {code_output: clean_code,history: _history,5 unmodified lineslast_content = _history[-1][1] if _history and len(_history[-1]) > 1 else ""modified_content = apply_search_replace_changes(last_content, clean_code)clean_content = remove_code_block(modified_content)_history.append([query, full_response])yield {code_output: clean_content,history: _history,1 unmodified linehistory_output: history_to_chatbot_messages(_history),}else:_history.append([query, full_response])yield {code_output: clean_code,history: _history,412 unmodified lines4134144154164174184191121 unmodified lines1541154215431544154515461547154876 unmodified lines162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711 unmodified line1673167416751676167716784 unmodified lines1683168416851686168716881 unmodified line1690169116921693169416955 unmodified lines1701170217031704170517061 unmodified line170817091710171117121713412 unmodified lines},{"name": "GLM-4.5","id": "zai-org/GLM-4.5","description": "GLM-4.5 model with thinking capabilities for advanced code generation"},{1121 unmodified linesreturn f"Error extracting website content: {str(e)}"stop_generation = Falsedef generation_code(query: Optional[str], image: Optional[gr.Image], file: Optional[str], website_url: Optional[str], _setting: Dict[str, str], _history: Optional[History], _current_model: Dict, enable_search: bool = False, language: str = "html", provider: str = "auto"):if query is None:76 unmodified lines# Enhance query with search if enabledenhanced_query = enhance_query_with_search(query, enable_search)# Check if this is GLM-4.5 model and handle with simple HuggingFace InferenceClientif _current_model["id"] == "zai-org/GLM-4.5":if image is not None:messages.append(create_multimodal_message(enhanced_query, image))else:messages.append({'role': 'user', 'content': enhanced_query})try:client = InferenceClient(provider="auto",api_key=os.environ["HF_TOKEN"],bill_to="huggingface",)stream = client.chat.completions.create(model="zai-org/GLM-4.5",messages=messages,stream=True,)content = ""for chunk in stream:if chunk.choices[0].delta.content:content += chunk.choices[0].delta.contentclean_code = remove_code_block(content)yield {code_output: gr.update(value=clean_code, language=get_gradio_language(language)),history_output: history_to_chatbot_messages(_history),sandbox: send_to_sandbox(clean_code) if language == "html" else "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your code using the download button above.</div>",}except Exception as e:content = f"Error with GLM-4.5: {str(e)}\n\nPlease make sure HF_TOKEN environment variable is set."clean_code = remove_code_block(content)_history.append([query, content])if language == "transformers.js":files = parse_transformers_js_output(clean_code)if files['index.html'] and files['index.js'] and files['style.css']:formatted_output = format_transformers_js_output(files)yield {code_output: formatted_output,history: _history,1 unmodified linehistory_output: history_to_chatbot_messages(_history),}else:yield {code_output: clean_code,history: _history,4 unmodified linesfiles = parse_svelte_output(clean_code)if files['src/App.svelte'] and files['src/app.css']:formatted_output = format_svelte_output(files)yield {code_output: formatted_output,history: _history,1 unmodified linehistory_output: history_to_chatbot_messages(_history),}else:yield {code_output: clean_code,history: _history,5 unmodified lineslast_content = _history[-1][1] if _history and len(_history[-1]) > 1 else ""modified_content = apply_search_replace_changes(last_content, clean_code)clean_content = remove_code_block(modified_content)yield {code_output: clean_content,history: _history,1 unmodified linehistory_output: history_to_chatbot_messages(_history),}else:yield {code_output: clean_code,history: _history,
app.py+25-296