Spaces:
Sleeping
Sleeping
Create app_old.py
Browse files- app_old.py +89 -0
app_old.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
from llama_cpp import Llama
|
| 5 |
+
|
| 6 |
+
# Путь для хранения модели локально в Space
|
| 7 |
+
model_dir = "./models"
|
| 8 |
+
os.makedirs(model_dir, exist_ok=True)
|
| 9 |
+
|
| 10 |
+
# Название модели и репозитория
|
| 11 |
+
repo_id = "Mykes/simpo_abl_model_epoch_1"
|
| 12 |
+
model_filename = "Simpo_Abl_Model_Epoch_1_Q8_0.gguf"
|
| 13 |
+
|
| 14 |
+
# Загружаем модель, если она еще не загружена
|
| 15 |
+
model_path = os.path.join(model_dir, model_filename)
|
| 16 |
+
if not os.path.exists(model_path):
|
| 17 |
+
print(f"Downloading model {model_filename} from {repo_id}...")
|
| 18 |
+
model_path = hf_hub_download(repo_id=repo_id, filename=model_filename, cache_dir=model_dir)
|
| 19 |
+
print(f"Model downloaded to {model_path}")
|
| 20 |
+
|
| 21 |
+
# Загружаем модель в память
|
| 22 |
+
print("Loading model into memory...")
|
| 23 |
+
llm = Llama(model_path=model_path, n_ctx=2048) # n_ctx - максимальная длина контекста
|
| 24 |
+
|
| 25 |
+
def respond(
|
| 26 |
+
message,
|
| 27 |
+
history: list[tuple[str, str]],
|
| 28 |
+
system_message,
|
| 29 |
+
max_tokens,
|
| 30 |
+
temperature,
|
| 31 |
+
top_p,
|
| 32 |
+
):
|
| 33 |
+
# Формируем историю сообщений в формате, подходящем для модели
|
| 34 |
+
messages = [{"role": "system", "content": system_message}]
|
| 35 |
+
|
| 36 |
+
for val in history:
|
| 37 |
+
if val[0]:
|
| 38 |
+
messages.append({"role": "user", "content": val[0]})
|
| 39 |
+
if val[1]:
|
| 40 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 41 |
+
|
| 42 |
+
messages.append({"role": "user", "content": message})
|
| 43 |
+
|
| 44 |
+
# Формируем prompt для модели (в GGUF обычно используется специфический формат)
|
| 45 |
+
prompt = ""
|
| 46 |
+
for msg in messages:
|
| 47 |
+
if msg["role"] == "system":
|
| 48 |
+
prompt += f"System: {msg['content']}\n"
|
| 49 |
+
elif msg["role"] == "user":
|
| 50 |
+
prompt += f"User: {msg['content']}\n"
|
| 51 |
+
elif msg["role"] == "assistant":
|
| 52 |
+
prompt += f"Assistant: {msg['content']}\n"
|
| 53 |
+
|
| 54 |
+
# Генерируем ответ с помощью модели
|
| 55 |
+
response = llm(
|
| 56 |
+
prompt,
|
| 57 |
+
max_tokens=max_tokens,
|
| 58 |
+
temperature=temperature,
|
| 59 |
+
top_p=top_p,
|
| 60 |
+
stop=["User:", "System:"], # Останавливаем генерацию, если начинается новое сообщение
|
| 61 |
+
stream=True
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Потоковая обработка ответа
|
| 65 |
+
full_response = ""
|
| 66 |
+
for chunk in response:
|
| 67 |
+
token = chunk["choices"][0]["text"]
|
| 68 |
+
full_response += token
|
| 69 |
+
yield full_response
|
| 70 |
+
|
| 71 |
+
# Настройка интерфейса Gradio
|
| 72 |
+
demo = gr.ChatInterface(
|
| 73 |
+
respond,
|
| 74 |
+
additional_inputs=[
|
| 75 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 76 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 77 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 78 |
+
gr.Slider(
|
| 79 |
+
minimum=0.1,
|
| 80 |
+
maximum=1.0,
|
| 81 |
+
value=0.95,
|
| 82 |
+
step=0.05,
|
| 83 |
+
label="Top-p (nucleus sampling)",
|
| 84 |
+
),
|
| 85 |
+
],
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
demo.launch()
|