sahanes commited on
Commit
9470817
·
verified ·
1 Parent(s): 4535d66

Delete backend

Browse files
backend/app/main.py DELETED
@@ -1,164 +0,0 @@
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
- Context:
134
- {' '.join(relevant_chunks)}
135
- Question: {question.text}
136
- """
137
-
138
- # Get response from OpenAI
139
- response = openai.chat.completions.create(
140
- model="gpt-4o-mini",
141
- messages=[
142
- {"role": "system", "content": "You are a helpful assistant that answers questions based on the provided context."},
143
- {"role": "user", "content": prompt}
144
- ]
145
- )
146
-
147
- return {"answer": response.choices[0].message.content}
148
-
149
- except Exception as e:
150
- raise HTTPException(status_code=500, detail=str(e))
151
-
152
- # Configure OpenAI API key
153
- load_dotenv()
154
- openai.api_key = os.getenv("OPENAI_API_KEY")
155
-
156
- if __name__ == "__main__":
157
- import uvicorn
158
- uvicorn.run(
159
- "main:app",
160
- host="0.0.0.0",
161
- port=8000,
162
- reload=True,
163
- log_level="info",
164
- workers=1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
backend/requirements.txt DELETED
@@ -1 +0,0 @@
1
- fastapi\nuvicorn\npython-multipart\nopenai\nfaiss-cpu\npypdf2\npython-dotenv
 
 
backend/uvicorn_config.py DELETED
@@ -1,31 +0,0 @@
1
- <<<<<<< HEAD
2
- from uvicorn.workers import UvicornWorker
3
-
4
-
5
- class CustomWorker(UvicornWorker):
6
- CONFIG_KWARGS = {
7
- "loop": "auto",
8
- "http": "auto",
9
- "ws": "auto",
10
- "lifespan": "auto",
11
- "log_level": "info",
12
- "access_log": True,
13
- "use_colors": True,
14
- "reload": True
15
- }
16
- =======
17
- from uvicorn.workers import UvicornWorker
18
-
19
-
20
- class CustomWorker(UvicornWorker):
21
- CONFIG_KWARGS = {
22
- "loop": "auto",
23
- "http": "auto",
24
- "ws": "auto",
25
- "lifespan": "auto",
26
- "log_level": "info",
27
- "access_log": True,
28
- "use_colors": True,
29
- "reload": True
30
- }
31
- >>>>>>> 467f9179b7ec187f353f256c52c2ae9e8be701b2