Spaces:
Sleeping
Sleeping
File size: 3,678 Bytes
7ba8eea 6ed3a7e 19838d1 6ed3a7e c0b4e54 7ba8eea bfe5e7e 6ba8184 925a414 daa1873 b156a95 7ba8eea 4c94084 7ba8eea 6ba8184 3424818 4c94084 7ba8eea 1665ba6 4c94084 daa1873 4c94084 6ed3a7e 7ba8eea 4c94084 6ed3a7e 7ba8eea 6ed3a7e 4c94084 7ba8eea 4c94084 |
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
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)
|