Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,9 @@
|
|
1 |
-
#
|
2 |
|
3 |
import os
|
4 |
import random
|
5 |
import time
|
6 |
|
7 |
-
# os.system("pip install gradio, llama_index, ragatouille, llama-cpp-python")
|
8 |
-
# os.system("git clone https://github.com/EnPaiva93/think-paraguayo-space-aux.git")
|
9 |
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")
|
10 |
|
11 |
from llama_cpp import Llama
|
@@ -14,7 +12,7 @@ from ragatouille import RAGPretrainedModel
|
|
14 |
from llama_index.core import Document, SimpleDirectoryReader
|
15 |
from llama_index.core.node_parser import SentenceSplitter
|
16 |
|
17 |
-
max_seq_length =
|
18 |
|
19 |
prompt = """Responde a preguntas de forma clara, amable, concisa y solamente en el lenguaje español, sobre el libro Ñande Ypykuéra.
|
20 |
Contexto
|
@@ -43,50 +41,21 @@ Contexto
|
|
43 |
|
44 |
# Initialize the LLM
|
45 |
llm = Llama(model_path="model.gguf",
|
46 |
-
n_ctx=
|
47 |
n_threads=2)
|
48 |
|
49 |
-
DOC_PATH = "/home/user/app/
|
50 |
|
51 |
-
print(os.listdir())
|
52 |
-
|
53 |
-
documents = SimpleDirectoryReader(input_files=["libro.txt"]).load_data()
|
54 |
-
|
55 |
-
parser = SentenceSplitter(chunk_size=128, chunk_overlap=64)
|
56 |
-
nodes = parser.get_nodes_from_documents(
|
57 |
-
documents, show_progress=False
|
58 |
-
)
|
59 |
-
list_nodes = [node.text for node in nodes]
|
60 |
-
|
61 |
-
print(os.getcwd())
|
62 |
-
|
63 |
-
# if os.path.exists(DOC_PATH):
|
64 |
RAG = RAGPretrainedModel.from_pretrained("AdrienB134/ColBERTv2.0-spanish-mmarcoES")
|
65 |
RAG = RAG.from_index(DOC_PATH, n_gpu=None)
|
66 |
RAG.search("init", None, k=1)
|
67 |
|
68 |
-
# else:
|
69 |
-
# RAG = RAGPretrainedModel.from_pretrained("AdrienB134/ColBERTv2.0-spanish-mmarcoES")
|
70 |
-
# my_documents = list_nodes
|
71 |
-
# index_path = RAG.index(index_name=DOC_PATH, max_document_length= 100, collection=my_documents)
|
72 |
-
|
73 |
-
# def convert_list_to_dict(lst):
|
74 |
-
# res_dct = {i: lst[i] for i in range(len(lst))}
|
75 |
-
# return res_dct
|
76 |
-
|
77 |
def reformat_rag(results_rag):
|
78 |
if results_rag is not None:
|
79 |
return [result["content"] for result in results_rag]
|
80 |
else:
|
81 |
return [""]
|
82 |
|
83 |
-
# def response(query: str = "Quien es gua'a?", context: str = ""):
|
84 |
-
# # print(base_prompt.format(query,""))
|
85 |
-
# inputs = tokenizer([base_prompt.format(query,"")], return_tensors = "pt").to("cuda")
|
86 |
-
# outputs = model.generate(**inputs, max_new_tokens = 128, temperature = 0.1, repetition_penalty=1.15, pad_token_id=tokenizer.eos_token_id)
|
87 |
-
# return tokenizer.batch_decode(outputs[0][inputs["input_ids"].shape[1]:].unsqueeze(0), skip_special_tokens=True)[0]
|
88 |
-
|
89 |
-
|
90 |
def chat_stream_completion(message, history):
|
91 |
|
92 |
context = reformat_rag(RAG.search(message, None, k=1))
|
@@ -98,50 +67,17 @@ def chat_stream_completion(message, history):
|
|
98 |
response = llm.create_completion(
|
99 |
prompt=full_prompt,
|
100 |
temperature=0.01,
|
101 |
-
max_tokens=
|
102 |
stream=True
|
103 |
)
|
104 |
-
|
105 |
-
# print(response)
|
106 |
|
107 |
message_repl = ""
|
108 |
for chunk in response:
|
109 |
if len(chunk['choices'][0]["text"]) != 0:
|
110 |
-
# print(chunk)
|
111 |
message_repl = message_repl + chunk['choices'][0]["text"]
|
112 |
yield message_repl
|
113 |
|
114 |
|
115 |
-
# def answer_question(pipeline, character, question):
|
116 |
-
# def answer_question(question):
|
117 |
-
# # context = reformat_rag(RAG.search(question, k=2))
|
118 |
-
# # context = " \n ".join(context)
|
119 |
-
# yield chat_stream_completion(question, None)
|
120 |
-
|
121 |
-
# def answer_question(question):
|
122 |
-
# context = reformat_rag(RAG.search(question, k=2))
|
123 |
-
# context = " \n ".join(context)
|
124 |
-
# return response(question, "")
|
125 |
-
|
126 |
-
# def random_element():
|
127 |
-
# return random.choice(list_nodes)
|
128 |
-
|
129 |
-
# clear_output()
|
130 |
-
print("Importación Completada.. OK")
|
131 |
-
|
132 |
-
css = """
|
133 |
-
h1 {
|
134 |
-
font-size: 32px;
|
135 |
-
text-align: center;
|
136 |
-
}
|
137 |
-
h2 {
|
138 |
-
text-align: center;
|
139 |
-
}
|
140 |
-
img {
|
141 |
-
height: 750px; /* Reducing the image height */
|
142 |
-
}
|
143 |
-
"""
|
144 |
-
|
145 |
def launcher():
|
146 |
with gr.Blocks(css=css) as demo:
|
147 |
gr.Markdown("# Think Paraguayo")
|
@@ -152,36 +88,15 @@ def launcher():
|
|
152 |
gr.Image(value="think_paraguayo.jpeg", type="filepath", label="Imagen Estática")
|
153 |
|
154 |
with gr.Column(scale=1):
|
155 |
-
|
156 |
-
# button = gr.Button("Cuentame ...")
|
157 |
-
# with gr.Row():
|
158 |
-
|
159 |
-
# textbox = gr.Textbox(label="", interactive=False, value=random_element())
|
160 |
-
# button.click(fn=random_element, inputs=[], outputs=textbox)
|
161 |
-
|
162 |
-
# with gr.Row():
|
163 |
chatbot = gr.ChatInterface(
|
164 |
fn=chat_stream_completion,
|
165 |
retry_btn = None,
|
166 |
stop_btn = None,
|
167 |
undo_btn = None
|
168 |
).queue()
|
169 |
-
# with gr.Row():
|
170 |
-
# msg = gr.Textbox()
|
171 |
-
# with gr.Row():
|
172 |
-
# clear = gr.ClearButton([msg, chatbot])
|
173 |
-
|
174 |
-
# def respond(message, chat_history):
|
175 |
-
# bot_message = answer_question(message)
|
176 |
-
# print(bot_message)
|
177 |
-
# chat_history.append((message, bot_message))
|
178 |
-
# time.sleep(2)
|
179 |
-
# return "", chat_history
|
180 |
-
|
181 |
-
# msg.submit(chat_stream_completion, [msg, chatbot], [msg, chatbot])
|
182 |
-
|
183 |
|
184 |
-
demo.launch()
|
185 |
|
186 |
if __name__ == "__main__":
|
187 |
launcher()
|
|
|
1 |
+
# Think Paraguayo
|
2 |
|
3 |
import os
|
4 |
import random
|
5 |
import time
|
6 |
|
|
|
|
|
7 |
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")
|
8 |
|
9 |
from llama_cpp import Llama
|
|
|
12 |
from llama_index.core import Document, SimpleDirectoryReader
|
13 |
from llama_index.core.node_parser import SentenceSplitter
|
14 |
|
15 |
+
max_seq_length = 256
|
16 |
|
17 |
prompt = """Responde a preguntas de forma clara, amable, concisa y solamente en el lenguaje español, sobre el libro Ñande Ypykuéra.
|
18 |
Contexto
|
|
|
41 |
|
42 |
# Initialize the LLM
|
43 |
llm = Llama(model_path="model.gguf",
|
44 |
+
n_ctx=max_seq_length,
|
45 |
n_threads=2)
|
46 |
|
47 |
+
DOC_PATH = "/home/user/app/index"
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
RAG = RAGPretrainedModel.from_pretrained("AdrienB134/ColBERTv2.0-spanish-mmarcoES")
|
50 |
RAG = RAG.from_index(DOC_PATH, n_gpu=None)
|
51 |
RAG.search("init", None, k=1)
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
def reformat_rag(results_rag):
|
54 |
if results_rag is not None:
|
55 |
return [result["content"] for result in results_rag]
|
56 |
else:
|
57 |
return [""]
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
def chat_stream_completion(message, history):
|
60 |
|
61 |
context = reformat_rag(RAG.search(message, None, k=1))
|
|
|
67 |
response = llm.create_completion(
|
68 |
prompt=full_prompt,
|
69 |
temperature=0.01,
|
70 |
+
max_tokens=max_seq_length,
|
71 |
stream=True
|
72 |
)
|
|
|
|
|
73 |
|
74 |
message_repl = ""
|
75 |
for chunk in response:
|
76 |
if len(chunk['choices'][0]["text"]) != 0:
|
|
|
77 |
message_repl = message_repl + chunk['choices'][0]["text"]
|
78 |
yield message_repl
|
79 |
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
def launcher():
|
82 |
with gr.Blocks(css=css) as demo:
|
83 |
gr.Markdown("# Think Paraguayo")
|
|
|
88 |
gr.Image(value="think_paraguayo.jpeg", type="filepath", label="Imagen Estática")
|
89 |
|
90 |
with gr.Column(scale=1):
|
91 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
chatbot = gr.ChatInterface(
|
93 |
fn=chat_stream_completion,
|
94 |
retry_btn = None,
|
95 |
stop_btn = None,
|
96 |
undo_btn = None
|
97 |
).queue()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
+
demo.launch()
|
100 |
|
101 |
if __name__ == "__main__":
|
102 |
launcher()
|