Spaces:
Sleeping
Sleeping
import gradio as gr | |
#|------------------------ | |
#| requirements.txt file | |
#|------------------------ | |
#| torch | |
#| transformers | |
import torch | |
import transformers | |
import gradio as gr | |
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline | |
from langchain.embeddings import HuggingFaceEmbeddings | |
from langchain.prompts import PromptTemplate | |
from langchain_openai import OpenAI | |
from langchain.llms import OpenAI | |
from langchain.chat_models import ChatOpenAI | |
from langchain.chains import RetrievalQA | |
from langchain.prompts import PromptTemplate | |
from langchain_openai import OpenAI | |
from langchain.llms import OpenAI | |
from transformers import AutoTokenizer,pipeline | |
from langchain.chat_models import ChatOpenAI | |
from langchain.vectorstores import Chroma | |
from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings | |
from langchain.embeddings import HuggingFaceEmbeddings | |
import os | |
api_key = os.getenv('OPEN_API_KEY') | |
client = OpenAI(api_key=api_key) | |
#HF_TOKEN = os.getenv('HF_TOKEN') | |
#OPENAI_API_KEY = os.getenv('OPENAI_API_KEY') | |
title = "QUESTION ANSWERIGN WITH RAG TECHNIQUES" | |
description = """ | |
This Space uses: | |
fine tuned model | |
ChatGPT 3.5 | |
to answer to quesiton extracted from Enron dataset | |
""" | |
examples=[ | |
['how Top Gas Gorillas is performing?'], | |
['Can the commission use revenues?'], | |
['....'], | |
] | |
#query2='how Top Gas Gorillas is performing?' | |
#context2='Note the discussion on market share halfway through the article. Top Gas Gorillas Show Strong Volume Growth The year 2000 was a banner year for the top players in gas marketing, with huge increases in gas prices, enormous volatility, continuing growth in sales volumes and major potential for profits.' | |
#model_name = "sentence-transformers/all-MiniLM-L6-v2" | |
model_name = 'sentence-transformers/multi-qa-mpnet-base-dot-v1' | |
#device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") | |
model_kwargs = {"device": "cpu"} | |
embeddings = HuggingFaceEmbeddings(model_name=model_name, model_kwargs=model_kwargs) | |
res_output=[] | |
def predict(query): | |
persist_directory="./chroma_db3/" | |
db2 = Chroma(persist_directory=persist_directory, embedding_function=embeddings) | |
db2.get() | |
docs = db2.similarity_search(query) | |
context=docs[0] | |
res_output.append(context) | |
# retrieve the context with LLM | |
question=query | |
context=docs[0].page_content | |
nlp_ft = pipeline("question-answering", model="FlavioBF/multi-qa-mpnet-base-dot-v1_fine_tuned_model") | |
res_output.append(nlp_ft(question=question,context=context)) | |
# Build prompt | |
template = """Use the context to answer the question at the end. If you don't know the answer, answer "I do not know the answer". Do not try to make up an answer. Use maximum two sentences maximum. \ | |
Keep the answer as concise as possible. | |
{context} | |
Question: {question} | |
Answer:""" | |
QA_CHAIN_PROMPT = PromptTemplate.from_template(template) | |
# retrieve the context | |
llm_name = "gpt-3.5-turbo" | |
llm = ChatOpenAI(model_name=llm_name, temperature=0,openai_api_key=api_key) | |
qa_chain = RetrievalQA.from_chain_type( | |
llm, | |
retriever=db2.as_retriever() | |
) | |
res_output.append(qa_chain({"query": question})) | |
return res_output | |
gr.Interface( | |
fn=predict, | |
inputs=[ | |
gr.Textbox(lines=2,label="Question"), | |
], | |
examples=examples, | |
title="Chat with Enron Dataset with RAG technique", | |
description=description, | |
outputs=[gr.Textbox(lines=8, label="CONTEXT"), | |
gr.Textbox(lines=4,label="multi-qa-mpnet-base-dot-v1_fine_tuned_model"), | |
gr.Textbox(lines=4, label="GPT 3.5 answer"), | |
] | |
).launch(share=True) #.launch(share=True) | |