Spaces:
Sleeping
Sleeping
Download app4.py from Alexey7878/Test2: direct link, hf CLI and curl.
- Browser
- Download file 1.98 kB
-
https://huggingface.co/spaces/Alexey7878/Test2/resolve/main/app4.py
- Command line
-
hf download hf://spaces/Alexey7878/Test2/app4.py
-
curl -L -o app4.py https://huggingface.co/spaces/Alexey7878/Test2/resolve/main/app4.py
1.98 kB
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| import os | |
| # Твой токен из переменной окружения | |
| token = os.getenv("Test2") | |
| # Клиент HF — используем text-generation | |
| client = InferenceClient( | |
| model="mistralai/Mistral-7B-Instruct-v0.3", | |
| token=token, | |
| provider="hf-inference" | |
| ) | |
| def build_prompt(system_message, history, user_message): | |
| """ | |
| Превращаем историю чата в обычный текстовый prompt, | |
| потому что text_generation НЕ поддерживает формат messages. | |
| """ | |
| prompt = f"System: {system_message}\n\n" | |
| for user, assistant in history: | |
| if user: | |
| prompt += f"User: {user}\n" | |
| if assistant: | |
| prompt += f"Assistant: {assistant}\n" | |
| prompt += f"User: {user_message}\nAssistant:" | |
| return prompt | |
| def respond(message, history, system_message, max_tokens, temperature, top_p): | |
| # Формируем текстовый prompt | |
| prompt = build_prompt(system_message, history, message) | |
| response = "" | |
| # Стримим text_generation | |
| for token in client.text_generation( | |
| prompt, | |
| max_new_tokens=max_tokens, | |
| stream=True, | |
| temperature=temperature, | |
| top_p=top_p, | |
| ): | |
| text = token.token.text | |
| response += text | |
| yield response | |
| # System prompt | |
| my_value2 = """ты вежливый чат бот, отвечай коротко и по делу на русском языке.""" | |
| demo = gr.ChatInterface( | |
| respond, | |
| additional_inputs=[ | |
| gr.Textbox(value=my_value2, label="System message"), | |
| gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), | |
| gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), | |
| gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"), | |
| ], | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |