Spaces:
Sleeping
Sleeping
another update
Browse files
app.py
CHANGED
|
@@ -5,12 +5,14 @@ import faiss
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
from bs4 import BeautifulSoup
|
| 7 |
import gradio as gr
|
| 8 |
-
import os
|
| 9 |
|
| 10 |
# Configure Gemini API key
|
| 11 |
GOOGLE_API_KEY = 'AIzaSyA0yLvySmj8xjMd0sedSgklg1fj0wBDyyw' # Replace with your API key
|
| 12 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
# Fetch lecture notes and model architectures
|
| 15 |
def fetch_lecture_notes():
|
| 16 |
lecture_urls = [
|
|
@@ -61,25 +63,22 @@ def initialize_faiss_index(embeddings):
|
|
| 61 |
return index
|
| 62 |
|
| 63 |
# Handle natural language queries
|
| 64 |
-
conversation_history = []
|
| 65 |
-
|
| 66 |
-
# Global variables
|
| 67 |
-
lecture_notes = fetch_lecture_notes()
|
| 68 |
-
model_architectures = fetch_model_architectures()
|
| 69 |
-
all_texts = lecture_notes + [model_architectures]
|
| 70 |
-
embedding_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
| 71 |
-
embeddings = create_embeddings(all_texts, embedding_model)
|
| 72 |
-
faiss_index = initialize_faiss_index(np.array(embeddings))
|
| 73 |
-
|
| 74 |
def handle_query(query, faiss_index, embeddings_texts, model):
|
|
|
|
|
|
|
| 75 |
query_embedding = model.encode([query]).astype('float32')
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
relevant_texts = [embeddings_texts[idx] for idx in indices[0]]
|
|
|
|
|
|
|
| 78 |
combined_text = "\n".join([text for text, _ in relevant_texts])
|
| 79 |
-
max_length = 500
|
| 80 |
if len(combined_text) > max_length:
|
| 81 |
combined_text = combined_text[:max_length] + "..."
|
| 82 |
|
|
|
|
| 83 |
try:
|
| 84 |
response = genai.generate_text(
|
| 85 |
model="models/text-bison-001",
|
|
@@ -88,21 +87,74 @@ def handle_query(query, faiss_index, embeddings_texts, model):
|
|
| 88 |
)
|
| 89 |
generated_text = response.result if response else "No response generated."
|
| 90 |
except Exception as e:
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
|
|
|
| 93 |
sources = [url for _, url in relevant_texts]
|
|
|
|
| 94 |
return generated_text, sources
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
def chatbot(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
response, sources = handle_query(message, faiss_index, all_texts, embedding_model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
|
|
|
| 100 |
if sources:
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
-
return history
|
| 106 |
|
| 107 |
iface = gr.ChatInterface(
|
| 108 |
chatbot,
|
|
@@ -113,12 +165,12 @@ iface = gr.ChatInterface(
|
|
| 113 |
"Explain the transformer architecture.",
|
| 114 |
"Tell me about datasets used to train LLMs.",
|
| 115 |
"How are LLM training datasets cleaned and preprocessed?",
|
|
|
|
| 116 |
],
|
| 117 |
retry_btn="Regenerate",
|
| 118 |
undo_btn="Undo",
|
| 119 |
clear_btn="Clear",
|
| 120 |
-
cache_examples=False, # Disable example caching to avoid file-related errors
|
| 121 |
)
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
-
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
from bs4 import BeautifulSoup
|
| 7 |
import gradio as gr
|
|
|
|
| 8 |
|
| 9 |
# Configure Gemini API key
|
| 10 |
GOOGLE_API_KEY = 'AIzaSyA0yLvySmj8xjMd0sedSgklg1fj0wBDyyw' # Replace with your API key
|
| 11 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 12 |
|
| 13 |
+
# Initialize conversation history
|
| 14 |
+
conversation_history = []
|
| 15 |
+
|
| 16 |
# Fetch lecture notes and model architectures
|
| 17 |
def fetch_lecture_notes():
|
| 18 |
lecture_urls = [
|
|
|
|
| 63 |
return index
|
| 64 |
|
| 65 |
# Handle natural language queries
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
def handle_query(query, faiss_index, embeddings_texts, model):
|
| 67 |
+
global conversation_history
|
| 68 |
+
|
| 69 |
query_embedding = model.encode([query]).astype('float32')
|
| 70 |
+
|
| 71 |
+
# Search FAISS index
|
| 72 |
+
_, indices = faiss_index.search(query_embedding, 3) # Retrieve top 3 results
|
| 73 |
relevant_texts = [embeddings_texts[idx] for idx in indices[0]]
|
| 74 |
+
|
| 75 |
+
# Combine relevant texts and truncate if necessary
|
| 76 |
combined_text = "\n".join([text for text, _ in relevant_texts])
|
| 77 |
+
max_length = 500 # Adjust as necessary
|
| 78 |
if len(combined_text) > max_length:
|
| 79 |
combined_text = combined_text[:max_length] + "..."
|
| 80 |
|
| 81 |
+
# Generate a response using Gemini
|
| 82 |
try:
|
| 83 |
response = genai.generate_text(
|
| 84 |
model="models/text-bison-001",
|
|
|
|
| 87 |
)
|
| 88 |
generated_text = response.result if response else "No response generated."
|
| 89 |
except Exception as e:
|
| 90 |
+
print(f"Error generating text: {e}")
|
| 91 |
+
generated_text = "An error occurred while generating the response."
|
| 92 |
+
|
| 93 |
+
# Update conversation history
|
| 94 |
+
conversation_history.append(f"User: {query}")
|
| 95 |
+
conversation_history.append(f"System: {generated_text}")
|
| 96 |
|
| 97 |
+
# Extract sources
|
| 98 |
sources = [url for _, url in relevant_texts]
|
| 99 |
+
|
| 100 |
return generated_text, sources
|
| 101 |
|
| 102 |
+
def generate_concise_response(prompt, context):
|
| 103 |
+
try:
|
| 104 |
+
response = genai.generate_text(
|
| 105 |
+
model="models/text-bison-001",
|
| 106 |
+
prompt=f"{prompt}\n\nContext: {context}\n\nAnswer:",
|
| 107 |
+
max_output_tokens=200
|
| 108 |
+
)
|
| 109 |
+
return response.result if response else "No response generated."
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print(f"Error generating concise response: {e}")
|
| 112 |
+
return "An error occurred while generating the concise response."
|
| 113 |
+
|
| 114 |
+
# Main function to execute the pipeline
|
| 115 |
def chatbot(message, history):
|
| 116 |
+
lecture_notes = fetch_lecture_notes()
|
| 117 |
+
model_architectures = fetch_model_architectures()
|
| 118 |
+
|
| 119 |
+
all_texts = lecture_notes + [model_architectures]
|
| 120 |
+
|
| 121 |
+
# Load the SentenceTransformers model
|
| 122 |
+
embedding_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
| 123 |
+
|
| 124 |
+
embeddings = create_embeddings(all_texts, embedding_model)
|
| 125 |
+
|
| 126 |
+
# Initialize FAISS index
|
| 127 |
+
faiss_index = initialize_faiss_index(np.array(embeddings))
|
| 128 |
+
|
| 129 |
response, sources = handle_query(message, faiss_index, all_texts, embedding_model)
|
| 130 |
+
print("Query:", message)
|
| 131 |
+
print("Response:", response)
|
| 132 |
+
|
| 133 |
+
# Format the response with conversation history
|
| 134 |
+
formatted_response = "Conversation History:\n\n"
|
| 135 |
+
for entry in conversation_history:
|
| 136 |
+
formatted_response += entry + "\n"
|
| 137 |
|
| 138 |
+
formatted_response += "\nCurrent Response:\n" + response
|
| 139 |
+
|
| 140 |
if sources:
|
| 141 |
+
print("Sources:", sources)
|
| 142 |
+
formatted_response += "\n\nSources:\n" + "\n".join(sources)
|
| 143 |
+
else:
|
| 144 |
+
print("Sources: None of the provided sources were used.")
|
| 145 |
+
|
| 146 |
+
# Generate a concise and relevant summary using Gemini
|
| 147 |
+
prompt = "Summarize the user queries so far"
|
| 148 |
+
user_queries_summary = " ".join([entry for entry in conversation_history if entry.startswith("User: ")])
|
| 149 |
+
concise_response = generate_concise_response(prompt, user_queries_summary)
|
| 150 |
+
print("Concise Response:")
|
| 151 |
+
print(concise_response)
|
| 152 |
+
|
| 153 |
+
formatted_response += "\n\nConcise Summary:\n" + concise_response
|
| 154 |
+
|
| 155 |
+
print("----")
|
| 156 |
|
| 157 |
+
return formatted_response
|
|
|
|
| 158 |
|
| 159 |
iface = gr.ChatInterface(
|
| 160 |
chatbot,
|
|
|
|
| 165 |
"Explain the transformer architecture.",
|
| 166 |
"Tell me about datasets used to train LLMs.",
|
| 167 |
"How are LLM training datasets cleaned and preprocessed?",
|
| 168 |
+
"Summarize the user queries so far"
|
| 169 |
],
|
| 170 |
retry_btn="Regenerate",
|
| 171 |
undo_btn="Undo",
|
| 172 |
clear_btn="Clear",
|
|
|
|
| 173 |
)
|
| 174 |
|
| 175 |
if __name__ == "__main__":
|
| 176 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|