Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +105 -0
- requirements.txt +13 -0
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from pprint import pprint
|
| 4 |
+
import bitsandbytes as bnb
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import torch
|
| 7 |
+
import torch.nn as nn
|
| 8 |
+
import transformers
|
| 9 |
+
from datasets import load_dataset
|
| 10 |
+
from huggingface_hub import notebook_login
|
| 11 |
+
from peft import (
|
| 12 |
+
LoraConfig,
|
| 13 |
+
PeftConfig,
|
| 14 |
+
PeftModel,
|
| 15 |
+
get_peft_model,
|
| 16 |
+
prepare_model_for_kbit_training,
|
| 17 |
+
)
|
| 18 |
+
from transformers import (
|
| 19 |
+
AutoConfig,
|
| 20 |
+
AutoModelForCausalLM,
|
| 21 |
+
AutoTokenizer,
|
| 22 |
+
BitsAndBytesConfig,
|
| 23 |
+
)
|
| 24 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
|
| 25 |
+
PEFT_MODEL = 'deedax/falcon-7b-personal-assistant'
|
| 26 |
+
|
| 27 |
+
config = PeftConfig.from_pretrained(PEFT_MODEL)
|
| 28 |
+
bnb_config = BitsAndBytesConfig(
|
| 29 |
+
load_in_4bit = True,
|
| 30 |
+
bnb_4bit_use_double_quant = True,
|
| 31 |
+
bnb_4bit_quant_type = 'nf4',
|
| 32 |
+
bnb_4bit_compute_dtype = torch.bfloat16,
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 36 |
+
config.base_model_name_or_path,
|
| 37 |
+
return_dict = True,
|
| 38 |
+
quantization_config = bnb_config,
|
| 39 |
+
device_map = 'auto',
|
| 40 |
+
trust_remote_code = True,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
| 44 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 45 |
+
|
| 46 |
+
model = PeftModel.from_pretrained(model, PEFT_MODEL)
|
| 47 |
+
model.config.use_cache = False
|
| 48 |
+
|
| 49 |
+
DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
| 50 |
+
|
| 51 |
+
generation_config = model.generation_config
|
| 52 |
+
generation_config.max_new_tokens = 200
|
| 53 |
+
generation_config.temperature = 0.1
|
| 54 |
+
generation_config.top_p = 0.3
|
| 55 |
+
generation_config.num_return_sequences = 1
|
| 56 |
+
generation_config.pad_token_id = tokenizer.eos_token_id
|
| 57 |
+
generation_config.eos_token_id = tokenizer.eos_token_id
|
| 58 |
+
|
| 59 |
+
def generate_response(question: str) -> str:
|
| 60 |
+
prompt = f'''
|
| 61 |
+
Below is a conversation between an interviewer and a candidate, You are Dahiru Ibrahim, the candidate.
|
| 62 |
+
Your contact details are as follows
|
| 63 |
+
github:https://github.com/Daheer
|
| 64 |
+
youtube:https://www.youtube.com/@deedaxinc
|
| 65 |
+
linkedin:https://linkedin.com/in/daheer-deedax
|
| 66 |
+
huggingface:https://huggingface.co/deedax
|
| 67 |
+
email:suhayrid6@gmail.com
|
| 68 |
+
phone:+2348147116750
|
| 69 |
+
Provide very SHORT, CONCISE, DIRECT and ACCURATE answers to the interview questions.
|
| 70 |
+
You do not respond as 'Interviewer' or pretend to be 'Interviewer'. You only respond ONCE as Candidate.
|
| 71 |
+
Interviewer: {question}
|
| 72 |
+
Candidate:
|
| 73 |
+
'''.strip()
|
| 74 |
+
encoding = tokenizer(prompt, return_tensors = 'pt').to(DEVICE)
|
| 75 |
+
with torch.inference_mode():
|
| 76 |
+
outputs = model.generate(
|
| 77 |
+
input_ids = encoding.input_ids,
|
| 78 |
+
attention_mask = encoding.attention_mask,
|
| 79 |
+
generation_config = generation_config,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens = True)
|
| 83 |
+
|
| 84 |
+
assistant_start = 'Candidate:'
|
| 85 |
+
response_start = response.find(assistant_start)
|
| 86 |
+
return response[response_start + len(assistant_start):].strip()
|
| 87 |
+
|
| 88 |
+
import streamlit as st
|
| 89 |
+
import random
|
| 90 |
+
|
| 91 |
+
st.title("💬 Deedax Chat (Falcon-7B-Instruct)")
|
| 92 |
+
if "messages" not in st.session_state:
|
| 93 |
+
st.session_state["messages"] = [{"role": "assistant", "content": "Ask me anything about Dahiru!"}]
|
| 94 |
+
|
| 95 |
+
for msg in st.session_state.messages:
|
| 96 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
| 97 |
+
|
| 98 |
+
if prompt := st.chat_input():
|
| 99 |
+
|
| 100 |
+
st.session_state.messages = []
|
| 101 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 102 |
+
st.chat_message("user").write(prompt)
|
| 103 |
+
msg = {'role': 'message', 'content': str(generate_response(prompt))}
|
| 104 |
+
st.session_state.messages.append(msg)
|
| 105 |
+
st.chat_message("assistant").write(msg['content'])
|
requirements.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pip
|
| 2 |
+
bitsandbytes
|
| 3 |
+
torch==2.0.1
|
| 4 |
+
git+https://github.com/huggingface/transformers.git@e03a9cc
|
| 5 |
+
git+https://github.com/huggingface/peft.git@42a184f
|
| 6 |
+
git+https://github.com/huggingface/accelerate.git@c9fbb71
|
| 7 |
+
datasets==2.12.0
|
| 8 |
+
loralib==0.1.1
|
| 9 |
+
einops==0.6.1
|
| 10 |
+
protobuf==3.20
|
| 11 |
+
jsonschema==3.0.2
|
| 12 |
+
transforms
|
| 13 |
+
streamlit
|