CoolFace
Apppublic

ShreyanshDave/InterviewPreparation

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py122 linesDownload Raw Back to root
1# Import necessary libraries2import pandas as pd3import numpy as np4import torch5import transformers6import langchain7from torch import bfloat168from transformers import StoppingCriteria, StoppingCriteriaList9 10# Set device to CPU explicitly11device = torch.device("cpu")12 13# Model Loading and Configuration14model_id = 'meta-llama/Llama-2-7b-chat-hf'15hf_auth = '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'  # Replace with your Hugging Face auth token16model_config = transformers.AutoConfig.from_pretrained(17    model_id,18    use_auth_token=hf_auth19)20bnb_config = transformers.BitsAndBytesConfig(21    load_in_4bit=True,22    bnb_4bit_quant_type='nf4',23    bnb_4bit_use_double_quant=True,24    bnb_4bit_compute_dtype=bfloat1625) 26 27model = transformers.AutoModelForCausalLM.from_pretrained(28    model_id,29    config=model_config,30    use_auth_token=hf_auth31)32model.to(device)  # Move the model to the CPU33model.eval()34print(f"Model loaded on {device}")35 36tokenizer = transformers.AutoTokenizer.from_pretrained(37    model_id,38    use_auth_token=hf_auth39)40 41# Stopping Criteria42stop_list = ['\nHuman:', '\n```\n']43stop_token_ids = [tokenizer(x)['input_ids'] for x in stop_list]44stop_token_ids = [torch.LongTensor(x).to(device) for x in stop_token_ids]45 46class StopOnTokens(StoppingCriteria):47    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:48        for stop_ids in stop_token_ids:49            if torch.eq(input_ids[0][-len(stop_ids):], stop_ids).all():50                return True51        return False52 53stopping_criteria = StoppingCriteriaList([StopOnTokens()])54 55generate_text = transformers.pipeline(56    model=model,57    tokenizer=tokenizer,58    return_full_text=True,59    task='text-generation',60    stopping_criteria=stopping_criteria,61    temperature=0.1,62    max_new_tokens=512,63    repetition_penalty=1.164)65 66# Testing the text generation67res = generate_text("Explain me the difference between Data Lakehouse and Data Warehouse.")68print(res[0]["generated_text"])69 70from langchain.llms import HuggingFacePipeline71llm = HuggingFacePipeline(pipeline=generate_text)72 73# Ingesting Data74#75df = pd.read_csv('interviewQnA.csv')76df.to_csv("output.csv", index=False)77 78from langchain.document_loaders.csv_loader import CSVLoader79loader = CSVLoader(file_path='interviewQnA.csv')80document = loader.load()81 82from langchain.text_splitter import RecursiveCharacterTextSplitter83text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=20)84all_splits = text_splitter.split_documents(document)85 86from langchain.embeddings import HuggingFaceEmbeddings87from langchain.vectorstores import FAISS88model_name = "sentence-transformers/all-mpnet-base-v2"89model_kwargs = {"device": "cpu"}  # Set to CPU90embeddings = HuggingFaceEmbeddings(model_name=model_name, model_kwargs=model_kwargs)91vectorstore = FAISS.from_documents(all_splits, embeddings)92 93from langchain.chains import ConversationalRetrievalChain94chain = ConversationalRetrievalChain.from_llm(llm, vectorstore.as_retriever(), return_source_documents=True)95 96# Define the function for interview evaluator97import gradio as gr98# Define the function99def interview_evaluator(question):100    # Initialize or load chat_history as needed101    chat_history = []102 103    # Process the user's question104    result = chain({"question": question, "chat_history": chat_history})105 106    # Return the answer107    return result['answer']108 109# Create a Gradio interface110iface = gr.Interface(111    fn=interview_evaluator,112    inputs=gr.Textbox(lines=2,label="Question", placeholder="Enter Question Here:"),113    outputs=gr.Textbox(label="Answer"),114    title= "CyberSage"115)116 117# Launch the Gradio interface118iface.launch()119 120 121 122