Spaces:
Paused
Paused
Wenye He
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,29 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndBytesConfig
|
| 3 |
import torch
|
| 4 |
import time # Added for timing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
MODEL_CONFIG = {
|
| 7 |
"phi-3": {
|
|
@@ -45,7 +68,13 @@ class ChatModel:
|
|
| 45 |
self.models[model_name] = model
|
| 46 |
self.tokenizers[model_name] = tokenizer
|
| 47 |
|
| 48 |
-
def generate(self, message, model_name, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
start_time = time.time() # Start timing
|
| 50 |
self.load_model(model_name)
|
| 51 |
config = MODEL_CONFIG[model_name]
|
|
@@ -86,7 +115,12 @@ def chat(message, history, model_choice):
|
|
| 86 |
return [(message, f"Error: {str(e)}")]
|
| 87 |
|
| 88 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 89 |
-
gr.Markdown("# 🚀 LLM Chatbot with Performance Metrics")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
with gr.Row():
|
| 91 |
model_choice = gr.Dropdown(
|
| 92 |
choices=["phi-3", "llama3-8b"],
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndBytesConfig
|
| 3 |
import torch
|
| 4 |
import time # Added for timing
|
| 5 |
+
# New imports
|
| 6 |
+
from langchain_community.document_loaders import PyPDFLoader, TextLoader
|
| 7 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 8 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 9 |
+
from langchain_community.vectorstores import FAISS
|
| 10 |
+
|
| 11 |
+
# Document processing function
|
| 12 |
+
def process_documents(files):
|
| 13 |
+
documents = []
|
| 14 |
+
for file in files:
|
| 15 |
+
if file.name.endswith(".pdf"):
|
| 16 |
+
loader = PyPDFLoader(file.name)
|
| 17 |
+
elif file.name.endswith(".txt"):
|
| 18 |
+
loader = TextLoader(file.name)
|
| 19 |
+
documents.extend(loader.load())
|
| 20 |
+
|
| 21 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=50)
|
| 22 |
+
texts = text_splitter.split_documents(documents)
|
| 23 |
+
|
| 24 |
+
embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-small-en-v1.5")
|
| 25 |
+
vectorstore = FAISS.from_documents(texts, embeddings)
|
| 26 |
+
return vectorstore
|
| 27 |
+
|
| 28 |
|
| 29 |
MODEL_CONFIG = {
|
| 30 |
"phi-3": {
|
|
|
|
| 68 |
self.models[model_name] = model
|
| 69 |
self.tokenizers[model_name] = tokenizer
|
| 70 |
|
| 71 |
+
def generate(self, message, model_name, history, vectorstore=None):
|
| 72 |
+
# RAG context retrieval
|
| 73 |
+
if vectorstore:
|
| 74 |
+
docs = vectorstore.similarity_search(message, k=3)
|
| 75 |
+
context = "\n".join([d.page_content for d in docs])
|
| 76 |
+
message = f"Context: {context}\n\nQuestion: {message}"
|
| 77 |
+
|
| 78 |
start_time = time.time() # Start timing
|
| 79 |
self.load_model(model_name)
|
| 80 |
config = MODEL_CONFIG[model_name]
|
|
|
|
| 115 |
return [(message, f"Error: {str(e)}")]
|
| 116 |
|
| 117 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 118 |
+
gr.Markdown("# 🚀 LLM Chatbot with RAG & Performance Metrics")
|
| 119 |
+
|
| 120 |
+
# Add document upload section
|
| 121 |
+
with gr.Row():
|
| 122 |
+
file_output = gr.File(label="Upload Documents", file_count="multiple",
|
| 123 |
+
file_types=[".pdf", ".txt"], max_size=10)
|
| 124 |
with gr.Row():
|
| 125 |
model_choice = gr.Dropdown(
|
| 126 |
choices=["phi-3", "llama3-8b"],
|