Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,63 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
""
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from langchain_community.llms import HuggingFaceHub
|
| 3 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 4 |
+
from langchain import hub
|
| 5 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
+
from langchain_community.vectorstores import Chroma
|
| 8 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
+
from rerankers import Reranker
|
| 11 |
+
import os
|
| 12 |
|
| 13 |
+
loader = PyPDFLoader("Constitucion_espa帽ola.pdf")
|
| 14 |
+
documents = loader.load()
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 17 |
+
docs_split = text_splitter.split_documents(documents)
|
| 18 |
|
| 19 |
+
embedding_function = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
vectordb = Chroma.from_documents(docs_split, embedding_function)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
client = InferenceClient("google/flan-t5-base", token=os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
| 24 |
|
| 25 |
+
ranker = Reranker("answerdotai/answerai-colbert-small-v1", model_type='colbert')
|
| 26 |
|
| 27 |
+
def generate_text(context, query):
|
| 28 |
+
inputs = f"Context: {context} Question: {query}"
|
| 29 |
+
response = client.chat_completion(inputs=inputs, task="text2text-generation")
|
| 30 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
def test_rag_reranking(query, ranker):
|
| 33 |
+
docs = vectordb.similarity_search_with_score(query)
|
| 34 |
+
context = []
|
| 35 |
+
for doc, score in docs:
|
| 36 |
+
if score < 7:
|
| 37 |
+
doc_details = doc.to_json()['kwargs']
|
| 38 |
+
context.append(doc_details['page_content'])
|
| 39 |
|
| 40 |
+
if len(context) > 0:
|
| 41 |
+
useful_context = context[0]
|
| 42 |
+
generation = generate_text(useful_context, query)
|
| 43 |
+
return generation
|
| 44 |
+
else:
|
| 45 |
+
return "No se encontr贸 informaci贸n suficiente para responder."
|
| 46 |
+
|
| 47 |
+
ranked = ranker.rerank(query=query, documents=raw_contexts, top_k=1)
|
| 48 |
+
|
| 49 |
+
best_context = ranked[0]["content"]
|
| 50 |
+
return generate_text(best_context, query)
|
| 51 |
+
|
| 52 |
+
def responder_chat(message, history):
|
| 53 |
+
respuesta = test_rag_reranking(message, ranker)
|
| 54 |
+
return respuesta
|
| 55 |
|
|
|
|
|
|
|
|
|
|
| 56 |
demo = gr.ChatInterface(
|
| 57 |
+
fn=responder_chat,
|
| 58 |
+
title="Chatbot sobre la constituci贸n espa帽ola",
|
| 59 |
+
theme="soft"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
|
|
|
| 62 |
if __name__ == "__main__":
|
| 63 |
demo.launch()
|