Shreyansh49
commited on
Commit
•
2228b85
1
Parent(s):
a4c4efe
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import transformers
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
from torch import cuda, bfloat16
|
8 |
+
from langchain.llms import HuggingFacePipeline
|
9 |
+
from langchain.document_loaders.csv_loader import CSVLoader
|
10 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
11 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
12 |
+
from langchain.vectorstores import FAISS
|
13 |
+
from langchain.chains import ConversationalRetrievalChain
|
14 |
+
|
15 |
+
model_id = 'meta-llama/Llama-2-7b-chat-hf'
|
16 |
+
device = f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu'
|
17 |
+
bnb_config = transformers.BitsAndBytesConfig(
|
18 |
+
load_in_4bit=True,
|
19 |
+
bnb_4bit_quant_type='nf4',
|
20 |
+
bnb_4bit_use_double_quant=True,
|
21 |
+
bnb_4bit_compute_dtype=bfloat16
|
22 |
+
)
|
23 |
+
hf_auth = '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'
|
24 |
+
model_config = transformers.AutoConfig.from_pretrained(
|
25 |
+
model_id,
|
26 |
+
use_auth_token=hf_auth
|
27 |
+
)
|
28 |
+
model = transformers.AutoModelForCausalLM.from_pretrained(
|
29 |
+
model_id,
|
30 |
+
trust_remote_code=True,
|
31 |
+
config=model_config,
|
32 |
+
quantization_config=bnb_config,
|
33 |
+
device_map='auto',
|
34 |
+
use_auth_token=hf_auth
|
35 |
+
)
|
36 |
+
model.eval()
|
37 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(
|
38 |
+
model_id,
|
39 |
+
use_auth_token=hf_auth
|
40 |
+
)
|
41 |
+
stop_list = ['\nHuman:', '\n```\n']
|
42 |
+
stop_token_ids = [tokenizer(x)['input_ids'] for x in stop_list]
|
43 |
+
stop_token_ids = [torch.LongTensor(x).to(device) for x in stop_token_ids]
|
44 |
+
class StopOnTokens(StoppingCriteria):
|
45 |
+
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
|
46 |
+
for stop_ids in stop_token_ids:
|
47 |
+
if torch.eq(input_ids[0][-len(stop_ids):], stop_ids).all():
|
48 |
+
return True
|
49 |
+
return False
|
50 |
+
stopping_criteria = StoppingCriteriaList([StopOnTokens()])
|
51 |
+
generate_text = transformers.pipeline(
|
52 |
+
model=model,
|
53 |
+
tokenizer=tokenizer,
|
54 |
+
return_full_text=True,
|
55 |
+
task='text-generation',
|
56 |
+
stopping_criteria=stopping_criteria,
|
57 |
+
temperature=0.1,
|
58 |
+
max_new_tokens=512,
|
59 |
+
repetition_penalty=1.1
|
60 |
+
)
|
61 |
+
llm = HuggingFacePipeline(pipeline=generate_text)
|
62 |
+
data = pd.read_json('interviewQna.json')
|
63 |
+
data.to_csv('interviewQna.csv', index=False)
|
64 |
+
df = pd.read_csv('interviewQna.csv')
|
65 |
+
df.to_csv("output.csv", index=False)
|
66 |
+
loader = CSVLoader(file_path='interviewQna.csv')
|
67 |
+
document = loader.load()
|
68 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=20)
|
69 |
+
all_splits = text_splitter.split_documents(document)
|
70 |
+
model_name = "sentence-transformers/all-mpnet-base-v2"
|
71 |
+
model_kwargs = {"device": "cuda"}
|
72 |
+
embeddings = HuggingFaceEmbeddings(model_name=model_name, model_kwargs=model_kwargs)
|
73 |
+
vectorstore = FAISS.from_documents(all_splits, embeddings)
|
74 |
+
chain = ConversationalRetrievalChain.from_llm(llm, vectorstore.as_retriever(), return_source_documents=True)
|
75 |
+
|
76 |
+
def interview_evaluator(question):
|
77 |
+
chat_history = []
|
78 |
+
result = chain({"question": question, "chat_history": chat_history})
|
79 |
+
return result['answer']
|
80 |
+
|
81 |
+
iface = gr.Interface(
|
82 |
+
fn=interview_evaluator,
|
83 |
+
inputs=gr.Textbox(lines=2,label="Question", placeholder="Enter Question Here:"),
|
84 |
+
outputs=gr.Textbox(label="Answer"),
|
85 |
+
title= "CyberSage"
|
86 |
+
)
|
87 |
+
|
88 |
+
if __name__ == "__main__":
|
89 |
+
iface.launch()
|