Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| import os | |
| # Инициализация клиента | |
| client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct", token=os.getenv("HF_TOKEN")) | |
| def process_text_simple(user_input, tone, task, language): | |
| if not user_input or not user_input.strip(): | |
| return "Пожалуйста, введите текст для обработки." | |
| prompt = f"Instruction: {task}. Tone: {tone}. Language: {language}.\nText: {user_input}" | |
| messages = [ | |
| {"role": "system", "content": "You are a helpful AI assistant. Respond only with the requested transformation."}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| full_response = "" | |
| try: | |
| # Добавляем строгую проверку каждого чанка | |
| for chunk in client.chat_completion(messages=messages, max_tokens=1000, stream=True): | |
| if chunk.choices and len(chunk.choices) > 0: # <-- Вот эта проверка уберет ошибку | |
| token = chunk.choices[0].delta.content | |
| if token: | |
| full_response += token | |
| yield full_response | |
| except Exception as e: | |
| yield f"Ошибка API: {str(e)}" | |
| # Создание интерфейса | |
| with gr.Blocks() as demo: | |
| gr.HTML("<div style='text-align:center;'><h1>🌈 AI Text Genius</h1><p>Версия 6.3 (Stable)</p></div>") | |
| with gr.Row(): | |
| with gr.Column(): | |
| if os.path.exists("logo.png"): | |
| gr.Image("logo.png", show_label=False, container=False, height=120) | |
| text_input = gr.Textbox(label="Введите текст", lines=5) | |
| with gr.Row(): | |
| tone = gr.Dropdown(["Нейтральный", "Официальный", "Дерзкий", "Дружелюбный"], label="Настроение", value="Нейтральный") | |
| task = gr.Dropdown(["Перевести", "Суммировать", "Улучшить стиль"], label="Задача", value="Перевести") | |
| lang = gr.Dropdown(["Русский", "English", "Deutsch"], label="Язык", value="Русский") | |
| btn = gr.Button("🚀 Обработать", variant="primary") | |
| with gr.Column(): | |
| output = gr.Textbox(label="Результат", lines=12, interactive=False) | |
| btn.click(fn=process_text_simple, inputs=[text_input, tone, task, lang], outputs=[output]) | |
| demo.launch(theme="soft") | |