Spaces:
Sleeping
Sleeping
File size: 2,977 Bytes
ef5cf7c 02c2107 ef5cf7c 02c2107 ef5cf7c 02c2107 f2c30d1 02c2107 f2c30d1 02c2107 f2c30d1 ef5cf7c 02c2107 f2c30d1 02c2107 f2c30d1 02c2107 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
from llama_cpp import Llama
import gradio as gr
import os
# Локальный путь, куда будет скачана модель при запуске
MODEL_PATH = "unsloth.Q4_K_M.gguf"
# Если модели ещё нет — скачиваем
if not os.path.exists(MODEL_PATH):
import requests
url = "https://huggingface.co/eliza555beth2002/DeepSeek-R1-Distill-Text2SQL-OneEpoch-GGUF-q4/resolve/main/unsloth.Q4_K_M.gguf"
print("📥 Скачиваю модель из Hugging Face...")
r = requests.get(url, stream=True)
with open(MODEL_PATH, "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
print("Модель скачана успешно!")
# Загружаем модель Llama.cpp
llm = Llama(
model_path=MODEL_PATH,
n_ctx=4096,
n_threads=4,
n_gpu_layers=0, # если на CPU — ставим 0
verbose=False,
)
# Основная функция
def text2sql(question):
schema = """
create table Authors(
AuthorID integer not null primary key check(AuthorID>0),
AuthorFIO varchar(40) not null
);
create table Books(
Cipher integer not null primary key check(Cipher>0),
BookName varchar(4000) not null,
BookTheme varchar(30) not null
check(BookTheme in('Любовь','Дружба','Смерть','Общественные проблемы','Внутренние противоречия')),
BookGenre varchar(15) not null
check(BookGenre in('Роман','Поэма','Рассказ','Пьеса','Эпопея','Драма'))
);
create table Wrote(
IDAuthor integer not null check(IDAuthor>0),
BookCipher integer not null check(BookCipher>0),
foreign key(IDAuthor) references Authors(AuthorID),
foreign key(BookCipher) references Books(Cipher),
primary key(IDAuthor, BookCipher)
);
"""
prompt = f"""Ты — помощник, который преобразует естественный язык в SQL.
Используй приведённую схему таблиц.
Вопрос: {question}
Контекст базы данных:
{schema}
Ответь ТОЛЬКО SQL-запросом в формате:
```sql
SELECT ...
```"""
# Генерация
output = llm(
prompt,
max_tokens=512,
temperature=0.2,
stop=["```"],
)
text = output["choices"][0]["text"].strip()
return text
# Интерфейс Gradio
demo = gr.Interface(
fn=text2sql,
inputs=gr.Textbox(label="Введите вопрос на естественном языке"),
outputs=gr.Textbox(label="Сгенерированный SQL-запрос"),
title="🧠 DeepSeek Text2SQL Demo",
description="Модель DeepSeek-R1-Distill преобразует текст в SQL-запрос по заданной схеме."
)
if __name__ == "__main__":
demo.launch()
|