diff --git a/gradio/gradio/chatbot/config.py b/gradio/gradio/chatbot/config.py index 4a71e372..e2cf7911 100644 --- a/gradio/gradio/chatbot/config.py +++ b/gradio/gradio/chatbot/config.py @@ -20,8 +20,27 @@ def get_config(key, default=None): return value def get_logger(name): - log = logging.getLogger(name) - return log + class Logger: + def __init__(self, name): + self.name = name + + def info(self, message): + print(f"[INFO] {self.name}: {message}") + sys.stdout.flush() + + def error(self, message): + print(f"[ERROR] {self.name}: {message}", file=sys.stderr) + sys.stderr.flush() + + def warning(self, message): + print(f"[WARNING] {self.name}: {message}") + sys.stdout.flush() + + def debug(self, message): + print(f"[DEBUG] {self.name}: {message}") + sys.stdout.flush() + + return Logger(name) def invalid_config(variable, error_message): "Log error for invalid configuration and quit" diff --git a/gradio/gradio/chatbot/main.py b/gradio/gradio/chatbot/main.py index e4975c66..72a0d7fe 100644 --- a/gradio/gradio/chatbot/main.py +++ b/gradio/gradio/chatbot/main.py @@ -8,6 +8,9 @@ # Function to stream responses from the LLM, maintaining context def query_llm(input_text, history): + # Log the user input + log.debug(f"User Input: {input_text}") + # Include the entire conversation history in the payload messages = [{"role": "user", "content": user_message} if i % 2 == 0 else {"role": "assistant", "content": assistant_message} for i, (user_message, assistant_message) in enumerate(history)] @@ -27,35 +30,40 @@ def query_llm(input_text, history): # Check if the response is valid if response.status_code == 200: - partial_response = "" - + full_response = "" + # Stream the response in chunks for line in response.iter_lines(): if line: # Decode and load the JSON from each 'data:' chunk line_str = line.decode('utf-8') - + # Skip the DONE message if line_str.strip() == "data: [DONE]": break - + if line_str.startswith("data:"): data_json = line_str[6:] # Remove 'data: ' prefix data = json.loads(data_json) - + # Extract and append content from the "delta" delta_content = data["choices"][0]["delta"].get("content", "") - partial_response += delta_content - + full_response += delta_content + # Yield the partial response so far - yield partial_response + yield full_response + + # Log the complete response after all chunks are received + log.debug(f"Assistant Full Response: {full_response}") else: - yield f"Error: {response.status_code}" + error_message = f"Error: {response.status_code}" + log.error(error_message) + yield error_message # Using gr.ChatInterface to handle streaming chatbot = gr.ChatInterface(fn=query_llm, title="LM Studio Chat") -print("Launching gradio interface ...") +print("Launching Gradio interface ...") sys.stdout.flush() - + chatbot.launch(server_name="0.0.0.0", server_port=7860)