Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,28 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import faiss
|
| 3 |
import numpy as np
|
| 4 |
import openai
|
| 5 |
-
from sentence_transformers import SentenceTransformer
|
| 6 |
-
from nltk.tokenize import sent_tokenize
|
| 7 |
-
import nltk
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
| 10 |
nltk.download('punkt')
|
| 11 |
-
nltk.download('
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Load the Ubuntu manual from a .txt file
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
full_text = file.read()
|
| 21 |
-
except FileNotFoundError:
|
| 22 |
-
raise FileNotFoundError(f"The file {manual_path} was not found.")
|
| 23 |
|
| 24 |
# Function to chunk the text into smaller pieces
|
| 25 |
def chunk_text(text, chunk_size=500):
|
|
@@ -42,99 +45,84 @@ def chunk_text(text, chunk_size=500):
|
|
| 42 |
# Apply chunking to the entire text
|
| 43 |
manual_chunks = chunk_text(full_text, chunk_size=500)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
tokenizer = AutoTokenizer.from_pretrained("microsoft/MiniLM-L12-H384-uncased")
|
| 54 |
-
model = AutoModel.from_pretrained("microsoft/MiniLM-L12-H384-uncased")
|
| 55 |
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def embed_text(text_list):
|
| 63 |
-
return np.array(embedding_model.encode(text_list), dtype=np.float32)
|
| 64 |
|
| 65 |
# Function to retrieve relevant chunks for a user query
|
| 66 |
def retrieve_chunks(query, k=5):
|
| 67 |
query_embedding = embed_text([query])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
# Function to truncate long inputs
|
| 87 |
-
def truncate_input(text, max_length=16385):
|
| 88 |
-
tokens = tokenizer.encode(text, truncation=True, max_length=max_length, return_tensors="pt")
|
| 89 |
-
return tokens
|
| 90 |
-
|
| 91 |
-
# Function to perform RAG: Retrieve chunks and generate a response
|
| 92 |
-
def rag_response(query, k=5, max_tokens=150):
|
| 93 |
-
try:
|
| 94 |
-
relevant_chunks, distances, indices = retrieve_chunks(query, k=k)
|
| 95 |
-
|
| 96 |
-
if not relevant_chunks:
|
| 97 |
-
return "Sorry, I couldn't find relevant information.", distances, indices
|
| 98 |
-
|
| 99 |
-
# Combine the query with retrieved chunks
|
| 100 |
-
augmented_input = query + "\n\n" + "\n\n".join(relevant_chunks)
|
| 101 |
-
|
| 102 |
-
# Truncate the input if it exceeds token limits
|
| 103 |
-
input_tokens = tokenizer.encode(augmented_input, return_tensors="pt")
|
| 104 |
-
if input_tokens.shape[1] > 16385:
|
| 105 |
-
# Truncate to fit within the model's maximum input length
|
| 106 |
-
augmented_input = tokenizer.decode(input_tokens[0, :16385])
|
| 107 |
-
|
| 108 |
-
# Generate response using OpenAI API
|
| 109 |
-
response = openai.ChatCompletion.create(
|
| 110 |
-
model="gpt-3.5-turbo",
|
| 111 |
-
messages=[
|
| 112 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
| 113 |
-
{"role": "user", "content": augmented_input}
|
| 114 |
-
],
|
| 115 |
-
max_tokens=max_tokens,
|
| 116 |
-
temperature=0.7
|
| 117 |
-
)
|
| 118 |
-
generated_text = response.choices[0].message['content'].strip()
|
| 119 |
-
return generated_text, distances, indices
|
| 120 |
-
except Exception as e:
|
| 121 |
-
return f"An error occurred: {e}", [], []
|
| 122 |
-
|
| 123 |
-
# Gradio Interface
|
| 124 |
-
|
| 125 |
-
# Gradio Interface
|
| 126 |
-
def format_output(response, distances, indices):
|
| 127 |
-
formatted_response = f"Response: {response}\n\nDistances: {distances}\n\nIndices: {indices}"
|
| 128 |
-
return formatted_response
|
| 129 |
-
|
| 130 |
-
iface = gr.Interface(
|
| 131 |
-
fn=rag_response,
|
| 132 |
-
inputs="text",
|
| 133 |
-
outputs="text",
|
| 134 |
-
title="RAG Chatbot with FAISS and GPT-3.5",
|
| 135 |
-
description="Ask me anything!",
|
| 136 |
-
live=True
|
| 137 |
-
)
|
| 138 |
|
|
|
|
| 139 |
if __name__ == "__main__":
|
| 140 |
iface.launch()
|
|
|
|
|
|
| 1 |
+
|
| 2 |
import gradio as gr
|
| 3 |
+
import nltk
|
| 4 |
+
from nltk.tokenize import sent_tokenize
|
| 5 |
+
from transformers import AutoTokenizer, AutoModel
|
| 6 |
+
import torch
|
| 7 |
import faiss
|
| 8 |
import numpy as np
|
| 9 |
import openai
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Set up OpenAI API key
|
| 12 |
+
openai.api_key = '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'
|
| 13 |
+
# Download NLTK data
|
| 14 |
nltk.download('punkt')
|
| 15 |
+
nltk.download('punkt-tab')
|
| 16 |
|
| 17 |
+
# Load the tokenizer and model
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/MiniLM-L12-H384-uncased")
|
| 19 |
+
model = AutoModel.from_pretrained("microsoft/MiniLM-L12-H384-uncased")
|
| 20 |
+
|
| 21 |
+
manual_path="ubuntu_manual.txt"
|
| 22 |
|
| 23 |
# Load the Ubuntu manual from a .txt file
|
| 24 |
+
with open(manual_path, "r", encoding="utf-8") as file:
|
| 25 |
+
full_text = file.read()
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Function to chunk the text into smaller pieces
|
| 28 |
def chunk_text(text, chunk_size=500):
|
|
|
|
| 45 |
# Apply chunking to the entire text
|
| 46 |
manual_chunks = chunk_text(full_text, chunk_size=500)
|
| 47 |
|
| 48 |
+
# Function to generate embeddings for each chunk
|
| 49 |
+
def embed_text(texts):
|
| 50 |
+
inputs = tokenizer(texts, padding=True, truncation=True, return_tensors="pt", max_length=512)
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
outputs = model(**inputs)
|
| 53 |
+
embeddings = outputs.last_hidden_state[:, 0, :].cpu().numpy() # CLS token representation
|
| 54 |
+
return embeddings
|
| 55 |
|
| 56 |
+
# Generate embeddings for the chunks
|
| 57 |
+
chunk_embeddings = embed_text(manual_chunks)
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# Convert embeddings to a numpy array
|
| 60 |
+
chunk_embeddings_np = np.array(chunk_embeddings)
|
| 61 |
|
| 62 |
+
# Create a FAISS index and add the embeddings
|
| 63 |
+
dimension = chunk_embeddings_np.shape[1]
|
| 64 |
+
index = faiss.IndexFlatL2(dimension)
|
| 65 |
+
index.add(chunk_embeddings_np)
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# Function to retrieve relevant chunks for a user query
|
| 68 |
def retrieve_chunks(query, k=5):
|
| 69 |
query_embedding = embed_text([query])
|
| 70 |
+
distances, indices = index.search(query_embedding, k=k)
|
| 71 |
+
valid_indices = [i for i in indices[0] if i < len(manual_chunks)]
|
| 72 |
+
relevant_chunks = [manual_chunks[i] for i in valid_indices]
|
| 73 |
+
return relevant_chunks
|
| 74 |
+
|
| 75 |
+
# Function to perform RAG: Retrieve chunks and generate a response using GPT-3.5
|
| 76 |
+
def rag_response_gpt3_5(query, k=3, max_tokens=150):
|
| 77 |
+
relevant_chunks = retrieve_chunks(query, k=k)
|
| 78 |
+
if not relevant_chunks:
|
| 79 |
+
return "Sorry, I couldn't find relevant information."
|
| 80 |
+
|
| 81 |
+
# Combine the query with a limited number of retrieved chunks
|
| 82 |
+
augmented_input = query + "\n" + "\n".join(relevant_chunks)
|
| 83 |
+
|
| 84 |
+
# Tokenize the augmented input and ensure it fits within model token limits
|
| 85 |
+
input_ids = tokenizer(augmented_input, return_tensors="pt").input_ids[0]
|
| 86 |
|
| 87 |
+
if len(input_ids) > 512:
|
| 88 |
+
input_ids = input_ids[:512]
|
| 89 |
+
augmented_input = tokenizer.decode(input_ids, skip_special_tokens=True)
|
| 90 |
+
|
| 91 |
+
response = openai.ChatCompletion.create(
|
| 92 |
+
model="gpt-3.5-turbo",
|
| 93 |
+
messages=[
|
| 94 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 95 |
+
{"role": "user", "content": augmented_input}
|
| 96 |
+
],
|
| 97 |
+
max_tokens=max_tokens,
|
| 98 |
+
temperature=0.7
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
return response.choices[0].message['content'].strip()
|
| 102 |
+
|
| 103 |
+
# Chat history to maintain conversation context
|
| 104 |
+
history = []
|
| 105 |
+
|
| 106 |
+
# Define Gradio interface function with chat history
|
| 107 |
+
def chatbot(query, history):
|
| 108 |
+
response = rag_response_gpt3_5(query)
|
| 109 |
+
history.append((query, response))
|
| 110 |
|
| 111 |
+
# Combine all messages into a single string
|
| 112 |
+
chat_history = ""
|
| 113 |
+
for user_input, bot_response in history:
|
| 114 |
+
chat_history += f"User: {user_input}\nBot: {bot_response}\n\n"
|
| 115 |
+
|
| 116 |
+
return chat_history, history
|
| 117 |
|
| 118 |
+
# Create the Gradio interface
|
| 119 |
+
iface = gr.Interface(fn=chatbot,
|
| 120 |
+
inputs=["text", "state"],
|
| 121 |
+
outputs=["text", "state"],
|
| 122 |
+
title="Ubuntu Manual Chatbot",
|
| 123 |
+
description="Ask me anything about the Ubuntu manual.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
# Launch the app
|
| 126 |
if __name__ == "__main__":
|
| 127 |
iface.launch()
|
| 128 |
+
|