Spaces:
Sleeping
Sleeping
File size: 4,720 Bytes
713092e 8c1a612 713092e 6ff00a5 a4aecbc 713092e 263fd54 713092e 263fd54 713092e 263fd54 713092e 263fd54 713092e 568558e 713092e 263fd54 713092e a4aecbc 713092e 263fd54 568558e 713092e 263fd54 a4aecbc 9244220 713092e 54a31b3 713092e 54a31b3 713092e 54a31b3 263fd54 713092e a4aecbc |
1 2 3 4 5 6 7 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 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
import google.generativeai as genai
import requests
import numpy as np
import faiss
from sentence_transformers import SentenceTransformer
from bs4 import BeautifulSoup
import gradio as gr
import os
# Configure Gemini API key
GOOGLE_API_KEY = 'AIzaSyA0yLvySmj8xjMd0sedSgklg1fj0wBDyyw' # Replace with your API key
genai.configure(api_key=GOOGLE_API_KEY)
# Fetch lecture notes and model architectures
def fetch_lecture_notes():
lecture_urls = [
"https://stanford-cs324.github.io/winter2022/lectures/introduction/",
"https://stanford-cs324.github.io/winter2022/lectures/capabilities/",
"https://stanford-cs324.github.io/winter2022/lectures/data/",
"https://stanford-cs324.github.io/winter2022/lectures/modeling/"
]
lecture_texts = []
for url in lecture_urls:
response = requests.get(url)
if response.status_code == 200:
print(f"Fetched content from {url}")
lecture_texts.append((extract_text_from_html(response.text), url))
else:
print(f"Failed to fetch content from {url}, status code: {response.status_code}")
return lecture_texts
def fetch_model_architectures():
url = "https://github.com/Hannibal046/Awesome-LLM#milestone-papers"
response = requests.get(url)
if response.status_code == 200:
print(f"Fetched model architectures, status code: {response.status_code}")
return extract_text_from_html(response.text), url
else:
print(f"Failed to fetch model architectures, status code: {response.status_code}")
return "", url
# Extract text from HTML content
def extract_text_from_html(html_content):
soup = BeautifulSoup(html_content, 'html.parser')
for script in soup(["script", "style"]):
script.extract()
text = soup.get_text(separator="\n", strip=True)
return text
# Generate embeddings using SentenceTransformers
def create_embeddings(texts, model):
texts_only = [text for text, _ in texts]
embeddings = model.encode(texts_only)
return embeddings
# Initialize FAISS index
def initialize_faiss_index(embeddings):
dimension = embeddings.shape[1] # Assuming all embeddings have the same dimension
index = faiss.IndexFlatL2(dimension)
index.add(embeddings.astype('float32'))
return index
# Handle natural language queries
conversation_history = []
# Global variables
lecture_notes = fetch_lecture_notes()
model_architectures = fetch_model_architectures()
all_texts = lecture_notes + [model_architectures]
embedding_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
embeddings = create_embeddings(all_texts, embedding_model)
faiss_index = initialize_faiss_index(np.array(embeddings))
def handle_query(query, faiss_index, embeddings_texts, model):
query_embedding = model.encode([query]).astype('float32')
_, indices = faiss_index.search(query_embedding, 3)
relevant_texts = [embeddings_texts[idx] for idx in indices[0]]
combined_text = "\n".join([text for text, _ in relevant_texts])
max_length = 500
if len(combined_text) > max_length:
combined_text = combined_text[:max_length] + "..."
try:
response = genai.generate_text(
model="models/text-bison-001",
prompt=f"Based on the following context:\n\n{combined_text}\n\nAnswer the following question: {query}",
max_output_tokens=200
)
generated_text = response.result if response else "No response generated."
except Exception as e:
generated_text = f"An error occurred while generating the response: {str(e)}"
sources = [url for _, url in relevant_texts]
return generated_text, sources
def chatbot(message, history):
response, sources = handle_query(message, faiss_index, all_texts, embedding_model)
total_text = response if response else "No response generated."
if sources:
relevant_source = "\n".join(sources)
total_text += f"\n\nSources:\n{relevant_source}"
history.append((message, total_text))
return history
iface = gr.ChatInterface(
chatbot,
title="LLM Research Assistant",
description="Ask questions about LLM architectures, datasets, and training techniques.",
examples=[
"What are some milestone model architectures in LLMs?",
"Explain the transformer architecture.",
"Tell me about datasets used to train LLMs.",
"How are LLM training datasets cleaned and preprocessed?",
],
retry_btn="Regenerate",
undo_btn="Undo",
clear_btn="Clear",
cache_examples=False, # Disable example caching to avoid file-related errors
)
if __name__ == "__main__":
iface.launch(server_name="0.0.0.0", server_port=7860)
|