import requests
import json
import secrets


class ModelClient:
    def __init__(self):
        self.session_hash = self._generate_random_hash()
        self.chat_history = []
        self.join_url = "https://yuntian-deng-chatgpt4turbo.hf.space/queue/join?__theme=light"
        self.stream_url = f"https://yuntian-deng-chatgpt4turbo.hf.space/queue/data?session_hash={self.session_hash}"
        self.headers = {
            "accept": "*/*",
            "accept-language": "ru,en;q=0.9",
            "content-type": "application/json",
            "priority": "u=1, i",
            "sec-ch-ua": "\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"YaBrowser\";v=\"24.7\", \"Yowser\";v=\"2.5\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin"
        }

    def _generate_random_hash(self, length=10):
        """Генерирует случайный хэш указанной длины"""
        characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        return ''.join(secrets.choice(characters) for _ in range(length))

    def _make_join_request(self, body):
        try:
            response = requests.post(self.join_url, headers=self.headers, json=body)
            response.raise_for_status()
        except requests.exceptions.HTTPError as e:
            print(f"HTTP error occurred: {e}")
        except Exception as e:
            print(f"An error occurred: {e}")
        return None

    def _make_stream_request(self):
        try:
            response = requests.get(self.stream_url, headers={**self.headers, 'accept': "text/event-stream"},
                                    stream=True)
            response.raise_for_status()
            return response
        except requests.exceptions.HTTPError as e:
            print(f"HTTP error occurred: {e}")
        except Exception as e:
            print(f"An error occurred: {e}")
        return None

    def _process_stream_response(self, response):
        response_message = ""
        if response:
            for chunk in response.iter_lines():
                if chunk:
                    try:
                        event_data = json.loads(chunk.decode('utf-8').replace("data: ", ""))
                        if event_data.get('msg') == "process_generating":
                            if event_data['output']['data'][0]:
                                output = event_data['output']['data'][0][-1]
                                response_message += output[-1]
                    except json.JSONDecodeError:
                        continue
        return response_message

    def get_response(self, message):
        body = {
            "data": [message, 1, 1, len(self.chat_history), self.chat_history, None],
            "event_data": None,
            "fn_index": 3,
            "trigger_id": 5,
            "session_hash": self.session_hash
        }
        self._make_join_request(body)
        stream_response = self._make_stream_request()
        response = self._process_stream_response(stream_response)
        self.chat_history.append([message, response])
        return response


import gradio as gr
from context_loader import ContextLoader
from models.gpt4o import ModelClient

conversation_themes = ["Новости", "Ссылки", "Даты", "Без контекста"]


class NeuroRavenApp:
    def __init__(self):
        self.system_prompt = None
        self.current_theme = None
        self.client = ModelClient()
        self.context_loader = ContextLoader()

    def handle_query(self, message, history, context_theme):
        if context_theme != self.current_theme:
            context = self.context_loader.load_txt_files(context_theme)
            if context:
                self.system_prompt = f"Отвечай на вопросы беря информацию из контекста: \n{context}"
            else:
                self.system_prompt = "You are a helpful assistant."

            self.current_theme = context_theme

        # response = self.client.get_response(
        #     message=message,
        #     history=history,
        #     sys_prompt=self.system_prompt
        # )
        response = self.client.get_response(
            message=message
        )
        return response

    def launch_app(self):
        gr.ChatInterface(
            self.handle_query,
            chatbot=gr.Chatbot(height=500),
            textbox=gr.Textbox(placeholder="Напиши свой вопрос", container=False, scale=7),
            title="NeuroRaven",
            theme="soft",
            retry_btn=None,
            clear_btn="Clear",
            additional_inputs=[
                gr.Dropdown(conversation_themes, label="Выбери тему контекста", value="Без контекста")
            ],
            css="footer{display:none !important}"
        ).launch()


if __name__ == "__main__":
    app = NeuroRavenApp()
    app.launch_app()

Как сделать что бы сообщение выводилось по chunk а не сразу всем текстом