Spaces:
Sleeping
Sleeping
Delete retriever.py
Browse files- retriever.py +0 -55
retriever.py
DELETED
|
@@ -1,55 +0,0 @@
|
|
| 1 |
-
from PyPDF2 import PdfReader
|
| 2 |
-
from google import genai
|
| 3 |
-
import time
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
# set up authentication
|
| 7 |
-
api_key = os.getenv("GEMINI_API_KEY")
|
| 8 |
-
client = genai.Client(api_key=api_key)
|
| 9 |
-
|
| 10 |
-
# read in documents
|
| 11 |
-
acled = PdfReader("ACLED.pdf")
|
| 12 |
-
dame = PdfReader("DAME.pdf")
|
| 13 |
-
nansen = PdfReader("Nansen.pdf")
|
| 14 |
-
|
| 15 |
-
# extract text from documents
|
| 16 |
-
acled_text = ""
|
| 17 |
-
|
| 18 |
-
dame_text = ""
|
| 19 |
-
|
| 20 |
-
nansen_text = ""
|
| 21 |
-
|
| 22 |
-
for page in acled.pages:
|
| 23 |
-
acled_text += page.extract_text()
|
| 24 |
-
|
| 25 |
-
for page in dame.pages:
|
| 26 |
-
dame_text += page.extract_text()
|
| 27 |
-
|
| 28 |
-
for page in nansen.pages:
|
| 29 |
-
nansen_text += page.extract_text()
|
| 30 |
-
|
| 31 |
-
# place extracted texts into a single list
|
| 32 |
-
documents = [acled_text, dame_text, nansen_text]
|
| 33 |
-
|
| 34 |
-
def retriever(prompt, history):
|
| 35 |
-
"""
|
| 36 |
-
Retrieves information from pre-loaded documents.
|
| 37 |
-
Args:
|
| 38 |
-
prompt: A string representing the search query
|
| 39 |
-
history: A placeholder representing query history
|
| 40 |
-
|
| 41 |
-
Returns:
|
| 42 |
-
Search results in natural language.
|
| 43 |
-
"""
|
| 44 |
-
context = "\n\n".join(documents)
|
| 45 |
-
super_prompt = f"{context}\n\nRetrieve information:\n\nQuestion: {prompt}. DO NOT precede your response with 'Answer:'. Just give the answer."
|
| 46 |
-
|
| 47 |
-
response = client.models.generate_content(
|
| 48 |
-
model="gemini-2.5-flash",
|
| 49 |
-
contents=super_prompt)
|
| 50 |
-
|
| 51 |
-
stream_text = ""
|
| 52 |
-
for each in response.text.split():
|
| 53 |
-
stream_text += each + " "
|
| 54 |
-
time.sleep(0.01)
|
| 55 |
-
yield stream_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|