-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
371 lines (305 loc) · 12.7 KB
/
app.py
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import uvicorn
import pyrebase
import shutil
import tempfile
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain.prompts import PromptTemplate
from fastapi import FastAPI, File, UploadFile,Form
from fastapi.responses import JSONResponse
from fastapi.exceptions import HTTPException
from models import *
from helper import *
from fastapi.middleware.cors import CORSMiddleware
import os
from dotenv import load_dotenv
from datetime import date
import google.generativeai as genai
import time
import firebase_admin
from firebase_admin import credentials, firestore,auth
import json
load_dotenv()
api_key = os.getenv('GEMINI_API')
genai.configure(api_key=api_key)
app = FastAPI(docs_url="/")
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/api/hello")
def read_root():
return {"message": "Hello World"}
if not firebase_admin._apps:
cred = credentials.Certificate(r"serviceAccountKey.json")
firebase_admin.initialize_app(cred)
firebaseConfig = {
"apiKey": os.getenv("FIREBASE_API_KEY"),
"authDomain": os.getenv("FIREBASE_AUTH_DOMAIN"),
"databaseURL": os.getenv("FIREBASE_DATABASE_URL"),
"projectId": os.getenv("FIREBASE_PROJECT_ID"),
"storageBucket": os.getenv("FIREBASE_STORAGE_BUCKET"),
"messagingSenderId": os.getenv("FIREBASE_MESSAGING_SENDER_ID"),
"appId": os.getenv("FIREBASE_APP_ID"),
"measurementId": os.getenv("FIREBASE_MEASUREMENT_ID")
}
firebase = pyrebase.initialize_app(firebaseConfig)
db = firestore.client()
@app.post('/signup')
async def create_an_account(user_data:SignUpSchema):
email = user_data.email
password = user_data.password
username = user_data.username
try:
user = auth.create_user(
display_name = username,
email = email,
password = password
)
return JSONResponse(content={"username" : user.display_name},
status_code= 201
)
except auth.EmailAlreadyExistsError:
raise HTTPException(
status_code=400,
detail= f"Account already created for the email {email}"
)
@app.post('/login')
async def create_access_token(user_data:LoginSchema):
email = user_data.email
password = user_data.password
try:
print("Inside try block")
user = firebase.auth().sign_in_with_email_and_password(
email = email,
password = password
)
token = user['idToken']
user_info = firebase.auth().get_account_info(token)
display_name = user_info['users'][0].get('displayName', '')
return JSONResponse(
content={
# "token":token
"username":display_name
},status_code=200
)
except:
raise HTTPException(
status_code=400,detail="Invalid Credentials"
)
@app.post("/notes/create")
async def create_note(note:NoteSchema):
new_note = db.collection('Notes').document(note.username).collection('notes').document()
new_note.set({
"noteTitle":note.noteTitle,
"noteText":note.noteText,
"creationDate":note.creationDate,
"noteKey": new_note.id
})
return {"noteKey": new_note.id}
@app.get("/notes/read")
async def read_notes(username:str):
notes = db.collection('Notes').document(username).collection('notes').get()
notes=[notes[i].to_dict() for i in range(len(notes))]
return notes
@app.put("/notes/update")
async def update_note(note: UpdateNoteSchema):
note_ref = db.collection('Notes').document(note.username).collection('notes').document(note.noteKey)
# Check if the document exists
if not note_ref.get().exists:
raise HTTPException(status_code=404, detail="Note not found")
# Update the note
note_ref.update({
"noteTitle": note.noteTitle,
"noteText": note.noteText
})
return {"message": "Note updated successfully"}
@app.delete("/notes/delete")
async def delete_notes(note: DeleteNoteSchema):
print("delete called")
db.collection('Notes').document(note.username).collection('notes').document(note.noteKey).delete()
return {"message":"Note deleted successfully"}
@app.post("/todo/create")
async def create_todo(todo_data: TodoSchema):
print("\n\nFunction called!!!\n\n")
new_todo_ref = db.collection('TodoList').document(todo_data.username).collection('Todos').document()
new_todo_ref.set({
"taskName": todo_data.taskName,
"taskDescription": todo_data.taskDescription,
"taskType": todo_data.taskType,
"dueDate": todo_data.dueDate,
"taskColor": todo_data.taskColor,
"isCompleted": todo_data.isCompleted,
"taskKey": new_todo_ref.id
})
return {"taskKey": new_todo_ref.id}
@app.get("/todo/read")
async def read_todos(username:str):
tasks = db.collection('TodoList').document(username).collection('Todos').get()
todos=[tasks[i].to_dict() for i in range(len(tasks))]
return todos
@app.delete("/todo/delete")
async def delete_todo(delete_todo: DeleteTodoSchema):
db.collection('TodoList').document(delete_todo.username).collection('Todos').document(delete_todo.taskKey).delete()
return {"message":"Note deleted successfully"}
@app.put("/todo/complete")
async def update_todo_completed(complete_todo: CompleteTodoSchema):
db.collection('TodoList').document(complete_todo.username).collection('Todos').document(complete_todo.taskKey).update({"isCompleted":complete_todo.isCompleted})
return {"message":"Task completed successfully"}
@app.get("/taskType/read")
async def read_task_types(username:str):
task_types = db.collection('TaskType').document(username).collection('taskType').get()
task_types = [task_types[i].to_dict() for i in range(len(task_types))]
return task_types
@app.post("/taskType/create")
async def create_task_type(taskType: TaskTypeSchema):
new_taskType = db.collection('TaskType').document(taskType.username).collection('taskType').document()
new_taskType.set({
"taskTypeName":taskType.taskTypeName,
"taskTypeColor":taskType.taskTypeColor,
"taskTypeKey": new_taskType.id
})
x = db.collection('TaskType').document(taskType.username).collection('taskType').get()
del x
return {"taskTypeKey": new_taskType.id}
# @app.delete("/taskType/delete")
# async def delete_task_type(deleteTaskType: DeleteTaskTypeScheme):
# db.collection('TaskType').document(deleteTaskType.username).collection('taskType').document(deleteTaskType.taskTypeKey).delete()
# return {"message":"Task Type deleted successfully"}
@app.delete("/taskType/delete")
async def delete_task_type(deleteTaskType: DeleteTaskTypeScheme):
doc_ref = db.collection('TaskType').document(deleteTaskType.username).collection('taskType').document(deleteTaskType.taskTypeKey)
doc = doc_ref.get()
if doc.exists:
doc_ref.delete()
return {"message": "Task Type deleted successfully"}
else:
raise HTTPException(status_code=404, detail="Task Type not found")
@app.post('/chat')
async def chat(userPrompt:ChatSchema):
tasks = db.collection('TodoList').document(userPrompt.username).collection('Todos').get()
todos=[tasks[i].to_dict() for i in range(len(tasks))]
model = genai.GenerativeModel('gemini-1.5-flash')
chat = model.start_chat(history=[])
if "manage my deadlines" in userPrompt.question.lower():
today = date.today()
prompt = generate_deadline_management_prompt(today, todos)
response = chat.send_message(prompt)
else:
response = chat.send_message(userPrompt.question)
return {"response": response.text}
@app.post("/upload-video")
async def process_video(file: UploadFile = File(...)):
model = genai.GenerativeModel('gemini-1.5-pro',
generation_config={"response_mime_type": "application/json",
"response_schema": VideoAnalysis})
prompt= vidPrompt()
with tempfile.TemporaryDirectory() as tmpdirname:
# Save the uploaded video file
file_location = os.path.join(tmpdirname, file.filename)
with open(file_location, "wb") as f:
shutil.copyfileobj(file.file, f)
print("File saved ")
# Upload the video file to Google Generative AI
video_file = genai.upload_file(path=file_location)
while video_file.state.name == "PROCESSING":
print('.', end='')
time.sleep(10)
video_file = genai.get_file(video_file.name)
if video_file.state.name == "FAILED":
raise ValueError(video_file.state.name)
# Generate content using the uploaded video and the prompt
print("Generating content...")
response = model.generate_content([video_file, prompt], request_options={"timeout": 600})
return json.loads(response.text)
@app.post("/flashcards")
async def generate_flashcards(file: UploadFile = File(...)):
FlashCardSchema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"question": {"type": "string"},
"answer": {"type": "string"},
"hint": {"type": "string"}
},
"required": ["question", "answer", "hint"]
}
}
# Create a temporary directory
with tempfile.TemporaryDirectory() as tmpdirname:
# Save the uploaded file
file_location = os.path.join(tmpdirname, file.filename)
with open(file_location, "wb") as f:
shutil.copyfileobj(file.file, f)
# Load the PDF
loader = PyPDFLoader(file_location)
docs = loader.load()
print("docs ready")
# Split the content into chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=2000)
splits = text_splitter.split_documents(docs)
print("split ready")
# Combine the chunks into a single string
content = []
for split in splits:
content.append(split.page_content)
content = '\n\n\n\n'.join(content)
print("content ready!")
# Define the prompt template
prompt_template = PromptTemplate(
input_variables=['notes'],
template='''Act as a teacher and consider the following text:
{notes}
Generate a list of question-answer pairs of varying difficulties.
The questions should be of type fill-in-the-blank or True/False.
The output should be a JSON array where each element contains "question", "answer", and "hint".
For example:
[
{{
"question": "What is the largest country in the world by land area?",
"answer": "Russia",
"hint": "It spans across two continents."
}},
{{
"question": "True or False: The Pacific Ocean is the largest ocean on Earth.",
"answer": "True",
"hint": "It's bigger than the Atlantic Ocean."
}}
]'''
)
model = genai.GenerativeModel('gemini-1.5-flash',
generation_config={"response_mime_type": "application/json",
"response_schema": FlashCardSchema})
# Format the prompt
prompt = prompt_template.format(notes=content)
# Generate the flashcards
response = model.generate_content(prompt)
# Return the generated flashcards as JSON
return json.loads(response.text)
@app.post("/imagesolver/")
async def generate_response(userPrompt:str = Form(default=""),file: UploadFile = File(...)):
# Create a temporary directory
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
with tempfile.TemporaryDirectory() as tmpdirname:
# Save the uploaded file
file_location = os.path.join(tmpdirname, file.filename)
with open(file_location, "wb") as f:
shutil.copyfileobj(file.file, f)
# Upload the image file to Google Generative AI
sample_file = genai.upload_file(path=file_location, display_name=file.filename)
if userPrompt=="":
prompt = "Solve the questions in the image."
# Generate content using the uploaded image and a prompt
else:
prompt = userPrompt
print(prompt)
response = model.generate_content([sample_file, prompt])
# Return the generated response as JSON
return JSONResponse(content={"response": response.text})
# if __name__ == "__main__":
# uvicorn.run("app:app",reload=True,port=8000)