Update app.py
Browse files
app.py
CHANGED
|
@@ -4,20 +4,9 @@ import fitz # PyMuPDF (takes PDF)
|
|
| 4 |
import re
|
| 5 |
from tqdm import tqdm
|
| 6 |
from openai import OpenAI
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
demo = gr.Interface(fn=greet, inputs="textbox", outputs="textbox")
|
| 13 |
-
|
| 14 |
-
if __name__ == "__main__":
|
| 15 |
-
demo.launch()
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
# Function to download PDF from a user-inputted URL
|
| 23 |
# Uses the urllib.request.urlretrieve function to fetch the PDF from the url and
|
|
@@ -86,29 +75,16 @@ def text_to_chunks(texts, word_length=150, start_page=1):
|
|
| 86 |
|
| 87 |
return chunks
|
| 88 |
|
| 89 |
-
# Example usage
|
| 90 |
-
file_path = 'GANPaper.pdf' # If directly uploaded
|
| 91 |
-
# output_path = 'downloaded_document.pdf'
|
| 92 |
-
# download_pdf(url, output_path)
|
| 93 |
-
|
| 94 |
texts = pdf_to_text(file_path, start_page=1)
|
| 95 |
chunks = text_to_chunks(texts, word_length=150)
|
| 96 |
|
| 97 |
-
# Optionally, print or process the chunks
|
| 98 |
-
for chunk in chunks[:5]: # Print first 5 chunks as a sample
|
| 99 |
-
print(chunk)
|
| 100 |
-
|
| 101 |
# Chunk Embedding:
|
| 102 |
|
| 103 |
-
from sentence_transformers import SentenceTransformer
|
| 104 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 105 |
|
| 106 |
# Assuming `chunks` is your list of preprocessed text chunks
|
| 107 |
embeddings = model.encode(chunks, show_progress_bar=True)
|
| 108 |
|
| 109 |
-
import faiss
|
| 110 |
-
import numpy as np
|
| 111 |
-
|
| 112 |
dimension = embeddings.shape[1] # Dimension of embeddings
|
| 113 |
index = faiss.IndexFlatL2(dimension) # L2 distance for similarity
|
| 114 |
index.add(embeddings.astype(np.float32)) # Add embeddings to index
|
|
@@ -123,16 +99,9 @@ def search(query, k=5):
|
|
| 123 |
distances, indices = index.search(np.array([query_embedding]), k)
|
| 124 |
return [chunks[idx] for idx in indices[0]]
|
| 125 |
|
| 126 |
-
# Example query
|
| 127 |
-
query = "Explain the concept of interaction in markets between local producers and local consumers."
|
| 128 |
-
results = search(query)
|
| 129 |
-
|
| 130 |
-
for result in results:
|
| 131 |
-
print(result)
|
| 132 |
|
| 133 |
# GPT Integration:
|
| 134 |
|
| 135 |
-
|
| 136 |
client = OpenAI(api_key='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')
|
| 137 |
|
| 138 |
|
|
@@ -169,14 +138,22 @@ def generate_response_from_chunks(chunks, user_query, max_tokens=250):
|
|
| 169 |
# Extracting and returning the text from the response
|
| 170 |
return response.choices[0].message.content.strip()
|
| 171 |
|
| 172 |
-
|
| 173 |
-
user_query = "Do generative adversarial nets require markov chains, and if not, why?"
|
| 174 |
relevant_chunks = search(user_query) # Assume `search` is a function that retrieves relevant academic text chunks
|
| 175 |
|
| 176 |
# Generate a cohesive response using the improved ChatGPT prompt structure
|
| 177 |
response = generate_response_from_chunks(relevant_chunks, user_query)
|
| 178 |
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
|
| 182 |
|
|
|
|
| 4 |
import re
|
| 5 |
from tqdm import tqdm
|
| 6 |
from openai import OpenAI
|
| 7 |
+
import faiss
|
| 8 |
+
import numpy as np
|
| 9 |
+
from sentence_transformers import SentenceTransformer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Function to download PDF from a user-inputted URL
|
| 12 |
# Uses the urllib.request.urlretrieve function to fetch the PDF from the url and
|
|
|
|
| 75 |
|
| 76 |
return chunks
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
texts = pdf_to_text(file_path, start_page=1)
|
| 79 |
chunks = text_to_chunks(texts, word_length=150)
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# Chunk Embedding:
|
| 82 |
|
|
|
|
| 83 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 84 |
|
| 85 |
# Assuming `chunks` is your list of preprocessed text chunks
|
| 86 |
embeddings = model.encode(chunks, show_progress_bar=True)
|
| 87 |
|
|
|
|
|
|
|
|
|
|
| 88 |
dimension = embeddings.shape[1] # Dimension of embeddings
|
| 89 |
index = faiss.IndexFlatL2(dimension) # L2 distance for similarity
|
| 90 |
index.add(embeddings.astype(np.float32)) # Add embeddings to index
|
|
|
|
| 99 |
distances, indices = index.search(np.array([query_embedding]), k)
|
| 100 |
return [chunks[idx] for idx in indices[0]]
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
# GPT Integration:
|
| 104 |
|
|
|
|
| 105 |
client = OpenAI(api_key='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')
|
| 106 |
|
| 107 |
|
|
|
|
| 138 |
# Extracting and returning the text from the response
|
| 139 |
return response.choices[0].message.content.strip()
|
| 140 |
|
| 141 |
+
|
|
|
|
| 142 |
relevant_chunks = search(user_query) # Assume `search` is a function that retrieves relevant academic text chunks
|
| 143 |
|
| 144 |
# Generate a cohesive response using the improved ChatGPT prompt structure
|
| 145 |
response = generate_response_from_chunks(relevant_chunks, user_query)
|
| 146 |
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
title = 'LilDewey'
|
| 151 |
+
|
| 152 |
+
demo = gr.Interface(fn=generate_response_from_chunks, inputs=[path, user_query] outputs=[answer])
|
| 153 |
+
|
| 154 |
+
demo.launch()
|
| 155 |
+
|
| 156 |
+
|
| 157 |
|
| 158 |
|
| 159 |
|