Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template_string, request, redirect, url_for
|
| 2 |
+
from huggingface_hub import snapshot_download
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# Доступные модели
|
| 9 |
+
AVAILABLE_MODELS = {
|
| 10 |
+
"GPT-OSS-20B": "togethercomputer/GPT-NeoXT-Chat-Base-20B",
|
| 11 |
+
"GPT-Neo-1.3B": "EleutherAI/gpt-neo-1.3B",
|
| 12 |
+
"LLaMA-7B": "meta-llama/Llama-2-7b-hf",
|
| 13 |
+
"Mistral-7B": "mistralai/Mistral-7B-v0.1"
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
# Папка для моделей
|
| 17 |
+
MODEL_DIR = "models"
|
| 18 |
+
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 19 |
+
|
| 20 |
+
# Активная модель
|
| 21 |
+
current_model = None
|
| 22 |
+
pipe = None
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@app.route("/", methods=["GET", "POST"])
|
| 26 |
+
def index():
|
| 27 |
+
global current_model, pipe
|
| 28 |
+
|
| 29 |
+
if request.method == "POST":
|
| 30 |
+
if "download" in request.form:
|
| 31 |
+
model_name = request.form["model"]
|
| 32 |
+
repo_id = AVAILABLE_MODELS[model_name]
|
| 33 |
+
|
| 34 |
+
# Скачиваем модель
|
| 35 |
+
snapshot_download(repo_id, local_dir=os.path.join(MODEL_DIR, model_name))
|
| 36 |
+
return redirect(url_for("index"))
|
| 37 |
+
|
| 38 |
+
elif "use" in request.form:
|
| 39 |
+
model_name = request.form["model"]
|
| 40 |
+
repo_id = AVAILABLE_MODELS[model_name]
|
| 41 |
+
|
| 42 |
+
# Загружаем модель
|
| 43 |
+
current_model = model_name
|
| 44 |
+
pipe = pipeline("text-generation", model=repo_id, device_map="auto")
|
| 45 |
+
return redirect(url_for("index"))
|
| 46 |
+
|
| 47 |
+
elif "ask" in request.form:
|
| 48 |
+
user_input = request.form["user_input"]
|
| 49 |
+
if pipe:
|
| 50 |
+
result = pipe(user_input, max_new_tokens=200, do_sample=True, temperature=0.7)
|
| 51 |
+
answer = result[0]["generated_text"]
|
| 52 |
+
else:
|
| 53 |
+
answer = "Модель не выбрана."
|
| 54 |
+
return render_template_string(TEMPLATE, models=AVAILABLE_MODELS, current=current_model, answer=answer)
|
| 55 |
+
|
| 56 |
+
return render_template_string(TEMPLATE, models=AVAILABLE_MODELS, current=current_model, answer=None)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
TEMPLATE = """
|
| 60 |
+
<!DOCTYPE html>
|
| 61 |
+
<html>
|
| 62 |
+
<head>
|
| 63 |
+
<title>AI Model Manager</title>
|
| 64 |
+
<style>
|
| 65 |
+
body { font-family: Arial; margin: 30px; background: #f4f4f4; }
|
| 66 |
+
.card { background: white; padding: 20px; margin-bottom: 20px; border-radius: 10px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);}
|
| 67 |
+
select, input[type=text], button { padding: 10px; margin-top: 10px; width: 100%; }
|
| 68 |
+
button { background: #007BFF; color: white; border: none; cursor: pointer; }
|
| 69 |
+
button:hover { background: #0056b3; }
|
| 70 |
+
</style>
|
| 71 |
+
</head>
|
| 72 |
+
<body>
|
| 73 |
+
<h2>AI Model Manager</h2>
|
| 74 |
+
|
| 75 |
+
<div class="card">
|
| 76 |
+
<form method="post">
|
| 77 |
+
<label for="model">Choose model:</label>
|
| 78 |
+
<select name="model">
|
| 79 |
+
{% for name, repo in models.items() %}
|
| 80 |
+
<option value="{{name}}" {% if current == name %}selected{% endif %}>{{name}} ({{repo}})</option>
|
| 81 |
+
{% endfor %}
|
| 82 |
+
</select>
|
| 83 |
+
<button type="submit" name="download">Download Model</button>
|
| 84 |
+
<button type="submit" name="use">Use Model</button>
|
| 85 |
+
</form>
|
| 86 |
+
<p><b>Current model:</b> {{ current if current else "None" }}</p>
|
| 87 |
+
</div>
|
| 88 |
+
|
| 89 |
+
<div class="card">
|
| 90 |
+
<form method="post">
|
| 91 |
+
<label>Ask AI:</label>
|
| 92 |
+
<input type="text" name="user_input" placeholder="Enter your question..." required>
|
| 93 |
+
<button type="submit" name="ask">Ask</button>
|
| 94 |
+
</form>
|
| 95 |
+
{% if answer %}
|
| 96 |
+
<div style="margin-top:15px; padding:10px; background:#e8f0fe; border-radius:5px;">
|
| 97 |
+
<b>Answer:</b> {{ answer }}
|
| 98 |
+
</div>
|
| 99 |
+
{% endif %}
|
| 100 |
+
</div>
|
| 101 |
+
</body>
|
| 102 |
+
</html>
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
app.run(debug=True, host="0.0.0.0", port=5000)
|