import time import gradio as gr import requests import json # Base URL of your API server; adjust host and port as needed API_URL = "http://10.168.232.93:8000" def reset_chat(system_prompt): """ Calls the /api/reset endpoint (POST) to initialize a new conversation. If system_prompt is provided, include it in the request body. Returns empty history and clears input. On error, shows error in chat. """ payload = {} if system_prompt: payload["system_prompt"] = system_prompt try: response = requests.post(f"{API_URL}/api/reset", json=payload) response.raise_for_status() except Exception as e: # Return error in chat if reset fails return [("Error resetting chat:", str(e))], "" # On successful reset, clear chat history and input return [], "" def stream_generate(history, message, temperature, repetition_penalty, top_p, top_k): """ Sends the user message and sampling parameters to /api/generate. Streams the response chunks and updates the last bot message in history. Clears input after sending. On error, shows error in chat. """ history = history + [(message, "")] yield history, "" payload = { "prompt": message, "temperature": temperature, "repetition_penalty": repetition_penalty, "top-p": top_p, "top-k": top_k } try: response = requests.post(f"{API_URL}/api/generate", json=payload, timeout=(3.05, None)) response.raise_for_status() except Exception as e: history[-1] = (message, f"Error: {str(e)}") yield history, "" return time.sleep(0.1) while True: time.sleep(0.01) response = requests.get( f"{API_URL}/api/generate_provider" ) data = response.json() chunk:str = data.get("response", "") done = data.get("done", False) if done: break if chunk.strip() == "": continue history[-1] = (message, history[-1][1] + chunk) yield history, "" print("end") def stop_generate(): try: requests.get(f"{API_URL}/api/stop") except Exception as e: print(e) # Build the Gradio interface optimized for PC with spacious layout # custom_css = """ # .gradio-container { # max-width: 1400px; # margin: auto; # padding: 20px; # } # .gradio-container > * { # margin-bottom: 20px; # } # #chatbox .overflow-y-auto { # height: 600px !important; # } # """ # Build the Gradio interface优化布局 with gr.Blocks(theme=gr.themes.Soft(font="Consolas"), fill_width=True) as demo: gr.Markdown("