Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from langchain import PromptTemplate, LLMChain
|
| 3 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 4 |
+
import os
|
| 5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
from langchain.schema import Document
|
| 8 |
+
from langchain_community.vectorstores import Qdrant
|
| 9 |
+
from langchain.chains import RetrievalQA
|
| 10 |
+
from langchain.llms import HuggingFaceHub
|
| 11 |
+
|
| 12 |
+
# Set up the RetrievalQA model
|
| 13 |
+
with open("brookline_data.txt", "r") as f:
|
| 14 |
+
data = f.read()
|
| 15 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=20)
|
| 16 |
+
splitted_data = text_splitter.split_text(data)
|
| 17 |
+
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-mpnet-base-v2')
|
| 18 |
+
retriever = Qdrant.as_retriever()
|
| 19 |
+
|
| 20 |
+
llm = HuggingFaceHub(repo_id="ahmadmac/Trained-T5-large", model_kwargs={"temperature": 0.5, "max_length": 512},huggingfacehub_api_token=)
|
| 21 |
+
qna = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
|
| 22 |
+
prompt_template = """ you are a highly knowledgeable AI assistant. Engage in a conversation with the user. Your main goal is to provide clear and informative answers to the user's questions.
|
| 23 |
+
User: {question}
|
| 24 |
+
Assistant:"""
|
| 25 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["question"])
|
| 26 |
+
chain = LLMChain(llm=llm, prompt=prompt)
|
| 27 |
+
|
| 28 |
+
def chatbot(question, chat_history):
|
| 29 |
+
result = qna(question)
|
| 30 |
+
if result['result']:
|
| 31 |
+
return result['result']
|
| 32 |
+
|
| 33 |
+
response = chain.run(question)
|
| 34 |
+
return response
|
| 35 |
+
|
| 36 |
+
demo = gr.ChatInterface(
|
| 37 |
+
fn=chatbot,
|
| 38 |
+
title="Chatbot",
|
| 39 |
+
description="AI Assistant!!"
|
| 40 |
+
)
|
| 41 |
+
demo.launch()
|