Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from pydantic import BaseModel
|
|
| 4 |
import time
|
| 5 |
import os
|
| 6 |
import base64
|
| 7 |
-
import
|
| 8 |
from google import genai
|
| 9 |
from google.genai import types
|
| 10 |
from typing import List
|
|
@@ -39,7 +39,7 @@ user = []
|
|
| 39 |
sessions = {} # Format: {uid: {conv_id: [history_list]}}
|
| 40 |
|
| 41 |
|
| 42 |
-
|
| 43 |
user_inst = [] #[[system prompt, About user, demanded tone, custum instruction]]
|
| 44 |
|
| 45 |
|
|
@@ -60,6 +60,40 @@ class VerifyRequest(BaseModel):
|
|
| 60 |
uid: str
|
| 61 |
idToken: str
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
@app.post("/api/verify")
|
| 65 |
def check_token(data: VerifyRequest):
|
|
@@ -73,6 +107,7 @@ def check_token(data: VerifyRequest):
|
|
| 73 |
user.append(uid)
|
| 74 |
Tokens.append(new_token)
|
| 75 |
sessions[uid] = {}
|
|
|
|
| 76 |
else:
|
| 77 |
idx = user.index(uid)
|
| 78 |
Tokens[idx] = new_token
|
|
@@ -114,6 +149,11 @@ def getConvId():
|
|
| 114 |
u = uuid.uuid4().bytes[:12]
|
| 115 |
return base64.urlsafe_b64encode(u).rstrip(b'=').decode()
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
def verify_access_token(auth: HTTPAuthorizationCredentials = Depends(security)):
|
| 119 |
token = auth.credentials
|
|
@@ -132,7 +172,7 @@ async def handleNewConv(new_conv: NewConv, token: str = Depends(verify_access_to
|
|
| 132 |
convs = sessions.get(user[i])
|
| 133 |
history = [
|
| 134 |
types.Content(role='model', parts=[types.Part(text=getSystemPrompt())]),
|
| 135 |
-
types.Content(role='user', parts=[types.Part(text=new_conv.prompt)])
|
| 136 |
]
|
| 137 |
text = call_gemini(history)
|
| 138 |
if text:
|
|
@@ -158,7 +198,7 @@ async def handleChat(chat_request: ChatRequest, token: str = Depends(verify_acce
|
|
| 158 |
types.Content(role=k[0], parts=[types.Part(text=k[1])])
|
| 159 |
for k in raw_history
|
| 160 |
]
|
| 161 |
-
history.append(types.Content(role='user', parts=[types.Part(text=chat_request.prompt)]))
|
| 162 |
# Generate response
|
| 163 |
text = call_gemini(history)
|
| 164 |
if text:
|
|
|
|
| 4 |
import time
|
| 5 |
import os
|
| 6 |
import base64
|
| 7 |
+
import uuidfrom datetime import datetime, timedelta, timezone
|
| 8 |
from google import genai
|
| 9 |
from google.genai import types
|
| 10 |
from typing import List
|
|
|
|
| 39 |
sessions = {} # Format: {uid: {conv_id: [history_list]}}
|
| 40 |
|
| 41 |
|
| 42 |
+
user_data = [] #[[name, gender, nationality, model name, model gender]]
|
| 43 |
user_inst = [] #[[system prompt, About user, demanded tone, custum instruction]]
|
| 44 |
|
| 45 |
|
|
|
|
| 60 |
uid: str
|
| 61 |
idToken: str
|
| 62 |
|
| 63 |
+
class UpdateSystemRequest(BaseModel):
|
| 64 |
+
category: str
|
| 65 |
+
content: str
|
| 66 |
+
|
| 67 |
+
def update_details():
|
| 68 |
+
user_data.append(['','','','',''])
|
| 69 |
+
user_inst.append(['','','',''])
|
| 70 |
+
|
| 71 |
+
@app.post("/update/system/userInfo")
|
| 72 |
+
async def update_Details(info: UpdateSystemRequest, token: str = Depends(verify_access_token)):
|
| 73 |
+
i = Tokens.index(token)
|
| 74 |
+
cat = info.category
|
| 75 |
+
val = info.content
|
| 76 |
+
if cat == 'name':
|
| 77 |
+
user_data[i][0] = val
|
| 78 |
+
if cat == 'gender':
|
| 79 |
+
user_data[i][1] = val
|
| 80 |
+
if cat == 'country':
|
| 81 |
+
user_data[i][2] = val
|
| 82 |
+
if cat == 'model_name':
|
| 83 |
+
user_data[i][3] = val
|
| 84 |
+
if cat == 'model_gender':
|
| 85 |
+
user_data[i][4] = val
|
| 86 |
+
if cat == 'system_prompt':
|
| 87 |
+
user_inst[i][0] =val
|
| 88 |
+
if cat == 'about_user':
|
| 89 |
+
user_inst[i][1] =val
|
| 90 |
+
if cat == 'tone':
|
| 91 |
+
user_inst[i][2] =val
|
| 92 |
+
if cat == 'custum_instruction':
|
| 93 |
+
user_inst[i][3] =val
|
| 94 |
+
else:
|
| 95 |
+
raise HTTPException(status_code=401, detail="Invalid Data.")
|
| 96 |
+
|
| 97 |
|
| 98 |
@app.post("/api/verify")
|
| 99 |
def check_token(data: VerifyRequest):
|
|
|
|
| 107 |
user.append(uid)
|
| 108 |
Tokens.append(new_token)
|
| 109 |
sessions[uid] = {}
|
| 110 |
+
update_details()
|
| 111 |
else:
|
| 112 |
idx = user.index(uid)
|
| 113 |
Tokens[idx] = new_token
|
|
|
|
| 149 |
u = uuid.uuid4().bytes[:12]
|
| 150 |
return base64.urlsafe_b64encode(u).rstrip(b'=').decode()
|
| 151 |
|
| 152 |
+
def currentTime():
|
| 153 |
+
# IST is UTC + 5:30
|
| 154 |
+
ist_offset = timezone(timedelta(hours=5, minutes=30))
|
| 155 |
+
current_time = datetime.now(ist_offset)
|
| 156 |
+
return current_time.strftime("%I:%M %p")
|
| 157 |
|
| 158 |
def verify_access_token(auth: HTTPAuthorizationCredentials = Depends(security)):
|
| 159 |
token = auth.credentials
|
|
|
|
| 172 |
convs = sessions.get(user[i])
|
| 173 |
history = [
|
| 174 |
types.Content(role='model', parts=[types.Part(text=getSystemPrompt())]),
|
| 175 |
+
types.Content(role='user', parts=[types.Part(text= f'time {currentTime()}\n{new_conv.prompt)}'])
|
| 176 |
]
|
| 177 |
text = call_gemini(history)
|
| 178 |
if text:
|
|
|
|
| 198 |
types.Content(role=k[0], parts=[types.Part(text=k[1])])
|
| 199 |
for k in raw_history
|
| 200 |
]
|
| 201 |
+
history.append(types.Content(role='user', parts=[types.Part(text= f'time {currentTime()}\n{chat_request.prompt)}']))
|
| 202 |
# Generate response
|
| 203 |
text = call_gemini(history)
|
| 204 |
if text:
|