Sambhavnoobcoder
commited on
Commit
•
3346531
1
Parent(s):
bf6f4ce
create app.py (#2)
Browse files- create app.py (40fabffc8f1c6e10bf7c3f9e0818e066650f649c)
app.py
ADDED
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import google.generativeai as genai
|
2 |
+
import requests
|
3 |
+
import numpy as np
|
4 |
+
import faiss
|
5 |
+
from sentence_transformers import SentenceTransformer
|
6 |
+
from bs4 import BeautifulSoup
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# Configure Gemini API key
|
10 |
+
gemini_api_secret_name = 'AIzaSyA0yLvySmj8xjMd0sedSgklg1fj0wBDyyw'
|
11 |
+
|
12 |
+
from google.colab import userdata
|
13 |
+
|
14 |
+
try:
|
15 |
+
GOOGLE_API_KEY = userdata.get(gemini_api_secret_name)
|
16 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
17 |
+
except userdata.SecretNotFoundError as e:
|
18 |
+
print(f'Secret not found\n\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\n\nVisit https://makersuite.google.com/app/apikey to create an API key\n\nStore that in the secrets section on the left side of the notebook (key icon)\n\nName the secret {gemini_api_secret_name}')
|
19 |
+
raise e
|
20 |
+
except userdata.NotebookAccessError as e:
|
21 |
+
print(f'You need to grant this notebook access to the {gemini_api_secret_name} secret in order for the notebook to access Gemini on your behalf.')
|
22 |
+
raise e
|
23 |
+
except Exception as e:
|
24 |
+
# unknown error
|
25 |
+
print(f"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://makersuite.google.com/app/apikey")
|
26 |
+
raise e
|
27 |
+
|
28 |
+
# Fetch lecture notes and model architectures
|
29 |
+
def fetch_lecture_notes():
|
30 |
+
lecture_urls = [
|
31 |
+
"https://stanford-cs324.github.io/winter2022/lectures/introduction/",
|
32 |
+
"https://stanford-cs324.github.io/winter2022/lectures/capabilities/",
|
33 |
+
"https://stanford-cs324.github.io/winter2022/lectures/data/",
|
34 |
+
"https://stanford-cs324.github.io/winter2022/lectures/modeling/"
|
35 |
+
]
|
36 |
+
lecture_texts = []
|
37 |
+
for url in lecture_urls:
|
38 |
+
response = requests.get(url)
|
39 |
+
if response.status_code == 200:
|
40 |
+
print(f"Fetched content from {url}")
|
41 |
+
lecture_texts.append((extract_text_from_html(response.text), url))
|
42 |
+
else:
|
43 |
+
print(f"Failed to fetch content from {url}, status code: {response.status_code}")
|
44 |
+
return lecture_texts
|
45 |
+
|
46 |
+
def fetch_model_architectures():
|
47 |
+
url = "https://github.com/Hannibal046/Awesome-LLM#milestone-papers"
|
48 |
+
response = requests.get(url)
|
49 |
+
if response.status_code == 200:
|
50 |
+
print(f"Fetched model architectures, status code: {response.status_code}")
|
51 |
+
return extract_text_from_html(response.text), url
|
52 |
+
else:
|
53 |
+
print(f"Failed to fetch model architectures, status code: {response.status_code}")
|
54 |
+
return "", url
|
55 |
+
|
56 |
+
# Extract text from HTML content
|
57 |
+
def extract_text_from_html(html_content):
|
58 |
+
soup = BeautifulSoup(html_content, 'html.parser')
|
59 |
+
for script in soup(["script", "style"]):
|
60 |
+
script.extract()
|
61 |
+
text = soup.get_text(separator="\n", strip=True)
|
62 |
+
return text
|
63 |
+
|
64 |
+
# Generate embeddings using SentenceTransformers
|
65 |
+
def create_embeddings(texts, model):
|
66 |
+
texts_only = [text for text, _ in texts]
|
67 |
+
embeddings = model.encode(texts_only)
|
68 |
+
return embeddings
|
69 |
+
|
70 |
+
# Initialize FAISS index
|
71 |
+
def initialize_faiss_index(embeddings):
|
72 |
+
dimension = embeddings.shape[1] # Assuming all embeddings have the same dimension
|
73 |
+
index = faiss.IndexFlatL2(dimension)
|
74 |
+
index.add(embeddings.astype('float32'))
|
75 |
+
return index
|
76 |
+
|
77 |
+
# Handle natural language queries
|
78 |
+
conversation_history = []
|
79 |
+
|
80 |
+
def handle_query(query, faiss_index, embeddings_texts, model):
|
81 |
+
global conversation_history
|
82 |
+
|
83 |
+
query_embedding = model.encode([query]).astype('float32')
|
84 |
+
|
85 |
+
# Search FAISS index
|
86 |
+
_, indices = faiss_index.search(query_embedding, 3) # Retrieve top 3 results
|
87 |
+
relevant_texts = [embeddings_texts[idx] for idx in indices[0]]
|
88 |
+
|
89 |
+
# Combine relevant texts and truncate if necessary
|
90 |
+
combined_text = "\n".join([text for text, _ in relevant_texts])
|
91 |
+
max_length = 500 # Adjust as necessary
|
92 |
+
if len(combined_text) > max_length:
|
93 |
+
combined_text = combined_text[:max_length] + "..."
|
94 |
+
|
95 |
+
# Generate a response using Gemini
|
96 |
+
try:
|
97 |
+
response = genai.generate_text(
|
98 |
+
model="models/text-bison-001",
|
99 |
+
prompt=f"Based on the following context:\n\n{combined_text}\n\nAnswer the following question: {query}",
|
100 |
+
max_output_tokens=200
|
101 |
+
)
|
102 |
+
generated_text = response.result
|
103 |
+
except Exception as e:
|
104 |
+
print(f"Error generating text: {e}")
|
105 |
+
generated_text = "An error occurred while generating the response."
|
106 |
+
|
107 |
+
# Update conversation history
|
108 |
+
conversation_history.append(f"User: {query}")
|
109 |
+
conversation_history.append(f"System: {generated_text}")
|
110 |
+
|
111 |
+
# Extract sources
|
112 |
+
sources = [url for _, url in relevant_texts]
|
113 |
+
|
114 |
+
return generated_text, sources
|
115 |
+
|
116 |
+
def generate_concise_response(prompt, context):
|
117 |
+
try:
|
118 |
+
response = genai.generate_text(
|
119 |
+
model="models/text-bison-001",
|
120 |
+
prompt=f"{prompt}\n\nContext: {context}\n\nAnswer:",
|
121 |
+
max_output_tokens=200
|
122 |
+
)
|
123 |
+
return response.result
|
124 |
+
except Exception as e:
|
125 |
+
print(f"Error generating concise response: {e}")
|
126 |
+
return "An error occurred while generating the concise response."
|
127 |
+
|
128 |
+
# Main function to execute the pipeline
|
129 |
+
def chatbot(message , history):
|
130 |
+
lecture_notes = fetch_lecture_notes()
|
131 |
+
model_architectures = fetch_model_architectures()
|
132 |
+
|
133 |
+
all_texts = lecture_notes + [model_architectures]
|
134 |
+
|
135 |
+
# Load the SentenceTransformers model
|
136 |
+
embedding_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
137 |
+
|
138 |
+
embeddings = create_embeddings(all_texts, embedding_model)
|
139 |
+
|
140 |
+
# Initialize FAISS index
|
141 |
+
faiss_index = initialize_faiss_index(np.array(embeddings))
|
142 |
+
|
143 |
+
|
144 |
+
response, sources = handle_query(message, faiss_index, all_texts, embedding_model)
|
145 |
+
print("Query:", message)
|
146 |
+
print("Response:", response)
|
147 |
+
total_text = response
|
148 |
+
if sources:
|
149 |
+
print("Sources:", sources)
|
150 |
+
relevant_source = ""
|
151 |
+
for source in sources:
|
152 |
+
relevant_source += source +"\n"
|
153 |
+
total_text += "\n\nSources:\n" + relevant_source
|
154 |
+
|
155 |
+
else:
|
156 |
+
print("Sources: None of the provided sources were used.")
|
157 |
+
print("----")
|
158 |
+
|
159 |
+
# Generate a concise and relevant summary using Gemini
|
160 |
+
prompt = "Summarize the user queries so far"
|
161 |
+
user_queries_summary = " ".join(message)
|
162 |
+
concise_response = generate_concise_response(prompt, user_queries_summary)
|
163 |
+
print("Concise Response:")
|
164 |
+
print(concise_response)
|
165 |
+
return total_text
|
166 |
+
|
167 |
+
iface = gr.ChatInterface(
|
168 |
+
chatbot,
|
169 |
+
title="LLM Research Assistant",
|
170 |
+
description="Ask questions about LLM architectures, datasets, and training techniques.",
|
171 |
+
examples=[
|
172 |
+
"What are some milestone model architectures in LLMs?",
|
173 |
+
"Explain the transformer architecture.",
|
174 |
+
"Tell me about datasets used to train LLMs.",
|
175 |
+
"How are LLM training datasets cleaned and preprocessed?",
|
176 |
+
"Summarize the user queries so far"
|
177 |
+
],
|
178 |
+
retry_btn="Regenerate",
|
179 |
+
undo_btn="Undo",
|
180 |
+
clear_btn="Clear",
|
181 |
+
)
|
182 |
+
|
183 |
+
if __name__ == "__main__":
|
184 |
+
iface.launch(debug=True)
|