|
|
|
|
|
import os |
|
import random |
|
import time |
|
|
|
os.system("pip install gradio, llama_index, ragatouille, llama-cpp-python") |
|
os.system("git clone https://github.com/EnPaiva93/think-paraguayo-space-aux.git") |
|
os.system("wget https://huggingface.co/thinkPy/gua-a_v0.2-dpo_mistral-7b_GGUF/resolve/main/gua-a_v0.2-dpo_mistral-7b_q4_K_M.gguf -O model.gguf") |
|
|
|
from llama_cpp import Llama |
|
import gradio as gr |
|
from ragatouille import RAGPretrainedModel |
|
from llama_index.core import Document, SimpleDirectoryReader |
|
from llama_index.core.node_parser import SentenceSplitter |
|
|
|
max_seq_length = 512 |
|
|
|
prompt = """Responde a preguntas de forma clara, amable, concisa y solamente en el lenguaje español, sobre el libro Ñande Ypykuéra. |
|
Contexto |
|
------------------------- |
|
{} |
|
------------------------- |
|
### Pregunta: |
|
{} |
|
### Respuesta: |
|
{}""" |
|
|
|
|
|
llm = Llama(model_path="model.gguf", |
|
n_ctx=512, |
|
n_threads=2) |
|
|
|
BASE_PATH = "/home/user/app/think-paraguayo-space-aux/" |
|
|
|
DOC_PATH = BASE_PATH+"index" |
|
|
|
print(os.listdir()) |
|
|
|
documents = SimpleDirectoryReader(input_files=[BASE_PATH+"libro.txt"]).load_data() |
|
|
|
parser = SentenceSplitter(chunk_size=128, chunk_overlap=64) |
|
nodes = parser.get_nodes_from_documents( |
|
documents, show_progress=False |
|
) |
|
list_nodes = [node.text for node in nodes] |
|
|
|
print(os.getcwd()) |
|
|
|
if os.path.exists(DOC_PATH): |
|
RAG = RAGPretrainedModel.from_index(DOC_PATH) |
|
else: |
|
RAG = RAGPretrainedModel.from_pretrained("AdrienB134/ColBERTv2.0-spanish-mmarcoES") |
|
my_documents = list_nodes |
|
index_path = RAG.index(index_name=DOC_PATH, max_document_length= 100, collection=my_documents) |
|
|
|
|
|
|
|
|
|
|
|
def reformat_rag(results_rag): |
|
if results_rag is not None: |
|
return [result["content"] for result in results_rag] |
|
else: |
|
return [""] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def chat_stream_completion(message, history): |
|
|
|
context = reformat_rag(RAG.search(message, k=1)) |
|
context = " \n ".join(context) |
|
|
|
full_prompt = prompt.format(context,message,"") |
|
print(full_prompt) |
|
|
|
response = llm.create_completion( |
|
prompt=full_prompt, |
|
temperature=0.01, |
|
max_tokens=256, |
|
stream=True |
|
) |
|
|
|
|
|
|
|
message_repl = "" |
|
for chunk in response: |
|
if len(chunk['choices'][0]["text"]) != 0: |
|
|
|
message_repl = message_repl + chunk['choices'][0]["text"] |
|
yield message_repl |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Importación Completada.. OK") |
|
|
|
css = """ |
|
h1 { |
|
font-size: 32px; |
|
text-align: center; |
|
} |
|
h2 { |
|
text-align: center; |
|
} |
|
img { |
|
height: 750px; /* Reducing the image height */ |
|
} |
|
""" |
|
|
|
def launcher(): |
|
with gr.Blocks(css=css) as demo: |
|
gr.Markdown("# Think Paraguayo") |
|
gr.Markdown("## Conoce la cultura guaraní!!") |
|
|
|
with gr.Row(variant='panel'): |
|
with gr.Column(scale=1): |
|
gr.Image(value=BASE_PATH+"think_paraguayo.jpeg", type="filepath", label="Imagen Estática") |
|
|
|
with gr.Column(scale=1): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chatbot = gr.ChatInterface( |
|
fn=chat_stream_completion, |
|
retry_btn = None, |
|
stop_btn = None, |
|
undo_btn = None |
|
).queue() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
demo.launch(share=True, inline= False, debug=True) |
|
|
|
if __name__ == "__main__": |
|
launcher() |