from flask import Flask, render_template_string, request, redirect, url_for from huggingface_hub import snapshot_download from transformers import pipeline import os app = Flask(__name__) # Доступные модели AVAILABLE_MODELS = { "GPT-OSS-20B": "togethercomputer/GPT-NeoXT-Chat-Base-20B", "GPT-Neo-1.3B": "EleutherAI/gpt-neo-1.3B", "LLaMA-7B": "meta-llama/Llama-2-7b-hf", "Mistral-7B": "mistralai/Mistral-7B-v0.1" } # Папка для моделей MODEL_DIR = "models" os.makedirs(MODEL_DIR, exist_ok=True) # Активная модель current_model = None pipe = None @app.route("/", methods=["GET", "POST"]) def index(): global current_model, pipe if request.method == "POST": if "download" in request.form: model_name = request.form["model"] repo_id = AVAILABLE_MODELS[model_name] # Скачиваем модель snapshot_download(repo_id, local_dir=os.path.join(MODEL_DIR, model_name)) return redirect(url_for("index")) elif "use" in request.form: model_name = request.form["model"] repo_id = AVAILABLE_MODELS[model_name] # Загружаем модель current_model = model_name pipe = pipeline("text-generation", model=repo_id, device_map="auto") return redirect(url_for("index")) elif "ask" in request.form: user_input = request.form["user_input"] if pipe: result = pipe(user_input, max_new_tokens=200, do_sample=True, temperature=0.7) answer = result[0]["generated_text"] else: answer = "Модель не выбрана." return render_template_string(TEMPLATE, models=AVAILABLE_MODELS, current=current_model, answer=answer) return render_template_string(TEMPLATE, models=AVAILABLE_MODELS, current=current_model, answer=None) TEMPLATE = """
Current model: {{ current if current else "None" }}