Spaces:
Runtime error
Runtime error
File size: 2,031 Bytes
a8ed80e 5dd6796 600d127 a8ed80e 5dd6796 d01a68b 600d127 5dd6796 db576dc 600d127 5dd6796 868109e a8ed80e |
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 |
import gradio as gr
import getpass
import os
from langchain.chains.question_answering import load_qa_chain
from langchain import HuggingFaceHub
from langchain_community.llms import HuggingFaceEndpoint
if "HUGGINGFACEHUB_API_TOKEN" not in os.environ:
os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv('hf_token')
from langchain.document_loaders import TextLoader
loader = TextLoader('./Agentville Academy.txt')
documents = loader.load()
import textwrap
def wrap_text_preserve_newlines(text, width=110):
# Split the input text into lines based on newline characters
lines = text.split('\n')
# Wrap each line individually
wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
# Join the wrapped lines back together using newline characters
wrapped_text = '\n'.join(wrapped_lines)
return wrapped_text
# Text Splitter
from langchain.text_splitter import CharacterTextSplitter
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
# Embeddings
from langchain.embeddings import HuggingFaceEmbeddings
embeddings = HuggingFaceEmbeddings()
# Vectorstore: https://python.langchain.com/en/latest/modules/indexes/vectorstores.html
from langchain.vectorstores import FAISS
db = FAISS.from_documents(docs, embeddings)
llm=HuggingFaceEndpoint(repo_id="mistralai/Mistral-7B-Instruct-v0.2", temperature=0.2, max_length=512)
chain = load_qa_chain(llm, chain_type="stuff")
def get_answer(query):
docs = db.similarity_search(query)
response = chain.run(input_documents=docs, question=query)
#return wrap_text_preserve_newlines(str(docs[0].page_content))
return response
demo = gr.Interface(fn=get_answer, inputs="text", theme=gr.themes.Glass(), outputs=gr.Markdown(), title="Agentville Academy: Where your Agent knowledge goes from zero to expert!", examples=['What is MemGPT?','Generate a learning plan for mastering autonomous agents','What is the difference between an LLM and an agent?'])
demo.launch()
|