Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,14 @@ import gradio as gr
|
|
| 3 |
import numpy as np
|
| 4 |
import faiss
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
-
from transformers import AutoTokenizer,
|
| 7 |
import torch
|
| 8 |
|
| 9 |
# =========================================================
|
| 10 |
# CONFIG
|
| 11 |
# =========================================================
|
| 12 |
EMBEDDING_MODEL = "all-MiniLM-L6-v2"
|
| 13 |
-
GENERATION_MODEL = "
|
| 14 |
TOP_K = 3
|
| 15 |
MAX_NEW_TOKENS = 200
|
| 16 |
|
|
@@ -67,7 +67,7 @@ def chunk_documents(documents, max_words=100):
|
|
| 67 |
return chunks
|
| 68 |
|
| 69 |
# =========================================================
|
| 70 |
-
#
|
| 71 |
# =========================================================
|
| 72 |
def build_vector_store(chunks):
|
| 73 |
embedder = SentenceTransformer(EMBEDDING_MODEL)
|
|
@@ -88,51 +88,45 @@ def retrieve(query, index, chunks, embedder):
|
|
| 88 |
return [chunks[i] for i in idxs[0]]
|
| 89 |
|
| 90 |
# =========================================================
|
| 91 |
-
# GENERATION ENGINE (
|
| 92 |
# =========================================================
|
| 93 |
class AnswerGenerator:
|
| 94 |
def __init__(self):
|
| 95 |
self.tokenizer = AutoTokenizer.from_pretrained(GENERATION_MODEL)
|
| 96 |
-
self.model =
|
| 97 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 98 |
self.model.to(self.device)
|
| 99 |
|
| 100 |
def generate(self, query, context_chunks):
|
| 101 |
context = "\n".join([c["text"] for c in context_chunks[:3]])
|
| 102 |
|
| 103 |
-
prompt =
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
"""
|
| 113 |
-
|
| 114 |
-
inputs = self.tokenizer(
|
| 115 |
-
prompt,
|
| 116 |
-
return_tensors="pt",
|
| 117 |
-
truncation=True,
|
| 118 |
-
max_length=512
|
| 119 |
)
|
|
|
|
|
|
|
| 120 |
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
| 121 |
|
| 122 |
with torch.no_grad():
|
| 123 |
outputs = self.model.generate(
|
| 124 |
**inputs,
|
| 125 |
max_new_tokens=MAX_NEW_TOKENS,
|
| 126 |
-
|
| 127 |
-
temperature=0.
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
no_repeat_ngram_size=3,
|
| 131 |
-
early_stopping=True
|
| 132 |
)
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
|
|
|
| 136 |
|
| 137 |
# =========================================================
|
| 138 |
# QUERY ENHANCEMENT
|
|
@@ -152,7 +146,7 @@ def enhance_query(query):
|
|
| 152 |
return query
|
| 153 |
|
| 154 |
# =========================================================
|
| 155 |
-
# INITIALIZE SYSTEM
|
| 156 |
# =========================================================
|
| 157 |
documents = load_documents()
|
| 158 |
chunks = chunk_documents(documents)
|
|
@@ -165,7 +159,7 @@ def answer_question(user_query):
|
|
| 165 |
return generator.generate(enhanced, retrieved)
|
| 166 |
|
| 167 |
# =========================================================
|
| 168 |
-
# CHATBOT RESPONSE
|
| 169 |
# =========================================================
|
| 170 |
def respond(message, history):
|
| 171 |
if not message or not message.strip():
|
|
@@ -176,16 +170,14 @@ def respond(message, history):
|
|
| 176 |
return history, history
|
| 177 |
|
| 178 |
# =========================================================
|
| 179 |
-
# CHATBOT UI
|
| 180 |
# =========================================================
|
| 181 |
with gr.Blocks(title="AI Twin Chatbot β Aniket Sirsikar") as demo:
|
| 182 |
|
| 183 |
gr.Markdown(
|
| 184 |
"""
|
| 185 |
-
# π€ AI Twin β Chatbot
|
| 186 |
**Product Manager | Data Analytics | AI-Powered Systems**
|
| 187 |
-
|
| 188 |
-
Ask me about my skills, projects, experience, education, or career goals.
|
| 189 |
"""
|
| 190 |
)
|
| 191 |
|
|
@@ -199,16 +191,7 @@ with gr.Blocks(title="AI Twin Chatbot β Aniket Sirsikar") as demo:
|
|
| 199 |
|
| 200 |
send = gr.Button("Send")
|
| 201 |
|
| 202 |
-
send.click(
|
| 203 |
-
|
| 204 |
-
inputs=[user_input, state],
|
| 205 |
-
outputs=[chatbot, state]
|
| 206 |
-
)
|
| 207 |
-
|
| 208 |
-
user_input.submit(
|
| 209 |
-
fn=respond,
|
| 210 |
-
inputs=[user_input, state],
|
| 211 |
-
outputs=[chatbot, state]
|
| 212 |
-
)
|
| 213 |
|
| 214 |
demo.queue().launch()
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import faiss
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
import torch
|
| 8 |
|
| 9 |
# =========================================================
|
| 10 |
# CONFIG
|
| 11 |
# =========================================================
|
| 12 |
EMBEDDING_MODEL = "all-MiniLM-L6-v2"
|
| 13 |
+
GENERATION_MODEL = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 14 |
TOP_K = 3
|
| 15 |
MAX_NEW_TOKENS = 200
|
| 16 |
|
|
|
|
| 67 |
return chunks
|
| 68 |
|
| 69 |
# =========================================================
|
| 70 |
+
# VECTOR STORE
|
| 71 |
# =========================================================
|
| 72 |
def build_vector_store(chunks):
|
| 73 |
embedder = SentenceTransformer(EMBEDDING_MODEL)
|
|
|
|
| 88 |
return [chunks[i] for i in idxs[0]]
|
| 89 |
|
| 90 |
# =========================================================
|
| 91 |
+
# GENERATION ENGINE (LLAMA β CAUSAL)
|
| 92 |
# =========================================================
|
| 93 |
class AnswerGenerator:
|
| 94 |
def __init__(self):
|
| 95 |
self.tokenizer = AutoTokenizer.from_pretrained(GENERATION_MODEL)
|
| 96 |
+
self.model = AutoModelForCausalLM.from_pretrained(GENERATION_MODEL)
|
| 97 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 98 |
self.model.to(self.device)
|
| 99 |
|
| 100 |
def generate(self, query, context_chunks):
|
| 101 |
context = "\n".join([c["text"] for c in context_chunks[:3]])
|
| 102 |
|
| 103 |
+
prompt = (
|
| 104 |
+
"<|system|>\n"
|
| 105 |
+
"You are an AI professional profile assistant.\n"
|
| 106 |
+
"Answer using ONLY the information provided.\n"
|
| 107 |
+
"Be concise and recruiter-facing.\n"
|
| 108 |
+
"<|user|>\n"
|
| 109 |
+
f"Information:\n{context}\n\n"
|
| 110 |
+
f"Question: {query}\n"
|
| 111 |
+
"<|assistant|>\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
)
|
| 113 |
+
|
| 114 |
+
inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024)
|
| 115 |
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
| 116 |
|
| 117 |
with torch.no_grad():
|
| 118 |
outputs = self.model.generate(
|
| 119 |
**inputs,
|
| 120 |
max_new_tokens=MAX_NEW_TOKENS,
|
| 121 |
+
do_sample=False,
|
| 122 |
+
temperature=0.2,
|
| 123 |
+
repetition_penalty=1.1,
|
| 124 |
+
eos_token_id=self.tokenizer.eos_token_id
|
|
|
|
|
|
|
| 125 |
)
|
| 126 |
|
| 127 |
+
decoded = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 128 |
+
answer = decoded.split("<|assistant|>")[-1].strip()
|
| 129 |
+
return answer
|
| 130 |
|
| 131 |
# =========================================================
|
| 132 |
# QUERY ENHANCEMENT
|
|
|
|
| 146 |
return query
|
| 147 |
|
| 148 |
# =========================================================
|
| 149 |
+
# INITIALIZE SYSTEM
|
| 150 |
# =========================================================
|
| 151 |
documents = load_documents()
|
| 152 |
chunks = chunk_documents(documents)
|
|
|
|
| 159 |
return generator.generate(enhanced, retrieved)
|
| 160 |
|
| 161 |
# =========================================================
|
| 162 |
+
# CHATBOT RESPONSE
|
| 163 |
# =========================================================
|
| 164 |
def respond(message, history):
|
| 165 |
if not message or not message.strip():
|
|
|
|
| 170 |
return history, history
|
| 171 |
|
| 172 |
# =========================================================
|
| 173 |
+
# CHATBOT UI
|
| 174 |
# =========================================================
|
| 175 |
with gr.Blocks(title="AI Twin Chatbot β Aniket Sirsikar") as demo:
|
| 176 |
|
| 177 |
gr.Markdown(
|
| 178 |
"""
|
| 179 |
+
# π€ AI Twin β Chatbot (LLaMA)
|
| 180 |
**Product Manager | Data Analytics | AI-Powered Systems**
|
|
|
|
|
|
|
| 181 |
"""
|
| 182 |
)
|
| 183 |
|
|
|
|
| 191 |
|
| 192 |
send = gr.Button("Send")
|
| 193 |
|
| 194 |
+
send.click(respond, [user_input, state], [chatbot, state])
|
| 195 |
+
user_input.submit(respond, [user_input, state], [chatbot, state])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
demo.queue().launch()
|