kenny commited on
Commit
ef609eb
·
0 Parent(s):

Initial commit

Browse files
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENAI_API_KEY=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
Dockerfile ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+ COPY ./app /code/app
7
+
8
+ RUN pip install --no-cache-dir -r requirements.txt
9
+
10
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
app/__pycache__/main.cpython-310.pyc ADDED
Binary file (4.56 kB). View file
 
app/__pycache__/main.cpython-312.pyc ADDED
Binary file (6.98 kB). View file
 
app/main.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from pydantic import BaseModel
4
+ import PyPDF2
5
+ import openai
6
+ import numpy as np
7
+ import faiss
8
+ import tiktoken
9
+ from typing import List
10
+ import io
11
+ from dotenv import load_dotenv
12
+ import os
13
+
14
+ app = FastAPI()
15
+
16
+ # Add CORS middleware
17
+ app.add_middleware(
18
+ CORSMiddleware,
19
+ allow_origins=["*"],
20
+ allow_credentials=True,
21
+ allow_methods=["*"],
22
+ allow_headers=["*"],
23
+ )
24
+
25
+ # In-memory storage
26
+
27
+
28
+ class DocumentStore:
29
+ def __init__(self):
30
+ self.documents: List[str] = []
31
+ self.embeddings = None
32
+ self.index = None
33
+
34
+ def reset(self):
35
+ self.documents = []
36
+ self.embeddings = None
37
+ self.index = None
38
+
39
+
40
+ doc_store = DocumentStore()
41
+
42
+
43
+ class Question(BaseModel):
44
+ text: str
45
+
46
+
47
+ def get_embedding(text: str) -> List[float]:
48
+ response = openai.embeddings.create(
49
+ model="text-embedding-3-small",
50
+ input=text
51
+ )
52
+ return response.data[0].embedding
53
+
54
+
55
+ def chunk_text(text: str, chunk_size: int = 1000) -> List[str]:
56
+ words = text.split()
57
+ chunks = []
58
+ current_chunk = []
59
+ current_size = 0
60
+
61
+ for word in words:
62
+ current_chunk.append(word)
63
+ current_size += len(word) + 1
64
+
65
+ if current_size >= chunk_size:
66
+ chunks.append(" ".join(current_chunk))
67
+ current_chunk = []
68
+ current_size = 0
69
+
70
+ if current_chunk:
71
+ chunks.append(" ".join(current_chunk))
72
+
73
+ return chunks
74
+
75
+
76
+ @app.post("/upload")
77
+ async def upload_pdf(file: UploadFile):
78
+ if not file.filename.endswith('.pdf'):
79
+ raise HTTPException(status_code=400, detail="File must be a PDF")
80
+
81
+ try:
82
+ # Reset the document store
83
+ doc_store.reset()
84
+
85
+ # Read PDF content
86
+ content = await file.read()
87
+ pdf_reader = PyPDF2.PdfReader(io.BytesIO(content))
88
+ text = ""
89
+ for page in pdf_reader.pages:
90
+ text += page.extract_text()
91
+
92
+ # Chunk the text
93
+ chunks = chunk_text(text)
94
+ doc_store.documents = chunks
95
+
96
+ # Create embeddings
97
+ embeddings = [get_embedding(chunk) for chunk in chunks]
98
+ doc_store.embeddings = np.array(embeddings, dtype=np.float32)
99
+
100
+ # Create FAISS index
101
+ dimension = len(embeddings[0])
102
+ doc_store.index = faiss.IndexFlatL2(dimension)
103
+ doc_store.index.add(doc_store.embeddings)
104
+
105
+ return {"message": "PDF processed successfully", "chunks": len(chunks)}
106
+
107
+ except Exception as e:
108
+ raise HTTPException(status_code=500, detail=str(e))
109
+
110
+
111
+ @app.post("/ask")
112
+ async def ask_question(question: Question):
113
+ if not doc_store.index:
114
+ raise HTTPException(
115
+ status_code=400, detail="No document has been uploaded yet")
116
+
117
+ try:
118
+ # Get question embedding
119
+ question_embedding = get_embedding(question.text)
120
+
121
+ # Search similar chunks
122
+ k = 10 # Number of relevant chunks to retrieve
123
+ D, I = doc_store.index.search(
124
+ np.array([question_embedding], dtype=np.float32), k)
125
+
126
+ # Get relevant chunks
127
+ relevant_chunks = [doc_store.documents[i] for i in I[0]]
128
+ print(relevant_chunks)
129
+
130
+ # Create prompt
131
+ prompt = f"""Based on the following context, please answer the question.
132
+ If the answer cannot be found in the context, say "I cannot find the answer in the document." You may also use the context to infer information that is not explicitly stated in the context. For example, if the context does not explicitly state what the paper is about, you may infer that the paper is about the topic of the question or the retrieved context.
133
+
134
+ Context:
135
+ {' '.join(relevant_chunks)}
136
+
137
+ Question: {question.text}
138
+ """
139
+
140
+ # Get response from OpenAI
141
+ response = openai.chat.completions.create(
142
+ model="gpt-4o-mini",
143
+ messages=[
144
+ {"role": "system", "content": "You are a helpful assistant that answers questions based on the provided context."},
145
+ {"role": "user", "content": prompt}
146
+ ]
147
+ )
148
+
149
+ return {"answer": response.choices[0].message.content}
150
+
151
+ except Exception as e:
152
+ raise HTTPException(status_code=500, detail=str(e))
153
+
154
+ # Configure OpenAI API key
155
+ load_dotenv()
156
+ openai.api_key = os.getenv("OPENAI_API_KEY")
157
+
158
+ if __name__ == "__main__":
159
+ import uvicorn
160
+ uvicorn.run(
161
+ "main:app",
162
+ host="0.0.0.0",
163
+ port=8000,
164
+ reload=True,
165
+ log_level="info",
166
+ workers=1
167
+ )
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ fastapi>=0.104.0
2
+ uvicorn[standard]>=0.24.0
3
+ python-multipart>=0.0.6
4
+ PyPDF2>=3.0.0
5
+ openai>=1.3.0
6
+ tiktoken>=0.5.0
7
+ faiss-cpu>=1.7.4
8
+ numpy>=1.24.0
9
+ python-dotenv>=1.0.0
uvicorn_config.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from uvicorn.workers import UvicornWorker
2
+
3
+
4
+ class CustomWorker(UvicornWorker):
5
+ CONFIG_KWARGS = {
6
+ "loop": "auto",
7
+ "http": "auto",
8
+ "ws": "auto",
9
+ "lifespan": "auto",
10
+ "log_level": "info",
11
+ "access_log": True,
12
+ "use_colors": True,
13
+ "reload": True
14
+ }