Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,94 +1,94 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
import
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
from PyPDF2 import PdfReader
|
| 5 |
+
import os
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
# Load environment variables
|
| 11 |
+
HUGGINGFACE_API_KEY = os.environ.get('HUGGINGFACE_API_KEY')
|
| 12 |
+
|
| 13 |
+
# Initialize Sentence Transformer for semantic search
|
| 14 |
+
sentence_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 15 |
+
|
| 16 |
+
# Load the Mistral model and tokenizer
|
| 17 |
+
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
| 20 |
+
|
| 21 |
+
# Function to extract text from PDF
|
| 22 |
+
def extract_text_from_pdf(pdf_path):
|
| 23 |
+
with open(pdf_path, 'rb') as file:
|
| 24 |
+
pdf_reader = PdfReader(file)
|
| 25 |
+
text = ""
|
| 26 |
+
for page in pdf_reader.pages:
|
| 27 |
+
text += page.extract_text()
|
| 28 |
+
return text
|
| 29 |
+
|
| 30 |
+
# Function to split text into chunks
|
| 31 |
+
def split_text(text, chunk_size=1000):
|
| 32 |
+
return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
|
| 33 |
+
|
| 34 |
+
# Load and process the Levo.ai PDF
|
| 35 |
+
pdf_path = "levo_ai_documentation.pdf" # Replace with the actual path to your PDF
|
| 36 |
+
levo_text = extract_text_from_pdf(pdf_path)
|
| 37 |
+
levo_chunks = split_text(levo_text)
|
| 38 |
+
|
| 39 |
+
# Encode chunks for semantic search
|
| 40 |
+
chunk_embeddings = sentence_model.encode(levo_chunks)
|
| 41 |
+
|
| 42 |
+
# Function to find most relevant chunks
|
| 43 |
+
def find_relevant_chunks(query, top_k=3):
|
| 44 |
+
query_embedding = sentence_model.encode([query])
|
| 45 |
+
similarities = cosine_similarity(query_embedding, chunk_embeddings)[0]
|
| 46 |
+
top_indices = np.argsort(similarities)[-top_k:][::-1]
|
| 47 |
+
return [levo_chunks[i] for i in top_indices]
|
| 48 |
+
|
| 49 |
+
# Function to generate response
|
| 50 |
+
def generate_response(prompt, context, max_length=1000):
|
| 51 |
+
full_prompt = f"""You are an advanced AI assistant for Levo.ai, a cutting-edge API security product. Your role is to provide accurate, helpful, and friendly support to users based on the following context from Levo.ai's documentation. Always maintain a professional and supportive tone.
|
| 52 |
+
|
| 53 |
+
Context:
|
| 54 |
+
{context}
|
| 55 |
+
|
| 56 |
+
User Query: {prompt}
|
| 57 |
+
Response:"""
|
| 58 |
+
inputs = tokenizer.encode(full_prompt, return_tensors="pt").to(model.device)
|
| 59 |
+
|
| 60 |
+
with torch.no_grad():
|
| 61 |
+
outputs = model.generate(
|
| 62 |
+
inputs,
|
| 63 |
+
max_length=max_length,
|
| 64 |
+
num_return_sequences=1,
|
| 65 |
+
temperature=0.7,
|
| 66 |
+
top_p=0.95,
|
| 67 |
+
do_sample=True
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 71 |
+
return response
|
| 72 |
+
|
| 73 |
+
# Streamlit UI
|
| 74 |
+
st.title("Levo.ai Support Chatbot")
|
| 75 |
+
|
| 76 |
+
if "messages" not in st.session_state:
|
| 77 |
+
st.session_state.messages = []
|
| 78 |
+
|
| 79 |
+
for message in st.session_state.messages:
|
| 80 |
+
with st.chat_message(message["role"]):
|
| 81 |
+
st.markdown(message["content"])
|
| 82 |
+
|
| 83 |
+
if prompt := st.chat_input("How can I assist you with Levo.ai today?"):
|
| 84 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 85 |
+
with st.chat_message("user"):
|
| 86 |
+
st.markdown(prompt)
|
| 87 |
+
|
| 88 |
+
relevant_chunks = find_relevant_chunks(prompt)
|
| 89 |
+
context = "\n".join(relevant_chunks)
|
| 90 |
+
|
| 91 |
+
response = generate_response(prompt, context)
|
| 92 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 93 |
+
with st.chat_message("assistant"):
|
| 94 |
+
st.markdown(response)
|