a9 commited on
Commit
f008773
·
verified ·
1 Parent(s): 2787e92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -51
app.py CHANGED
@@ -1,16 +1,54 @@
1
- from fastapi import FastAPI, Response, Cookie
2
- from pydantic import BaseModel, Field
3
- import time, os, base64, uuid, requests
 
 
 
 
4
  from google import genai
5
  from google.genai import types
6
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  app = FastAPI()
9
 
10
 
11
  Tokens = []
12
- # In-memory storage (Better to use a Dict)
13
- sessions = {} # Format: {token: {conv_id: [history_list]}}
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  class ChatRequest(BaseModel):
16
  prompt: str
@@ -19,84 +57,98 @@ class ChatRequest(BaseModel):
19
  class NewConv(BaseModel):
20
  prompt: str
21
 
22
- def gen(prompt):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  try:
24
- response = client.models.generate_content(model="gemma-3-4b-it", contents=prompt)
 
 
 
25
  return response.text
26
  except Exception as e:
27
- print(f"API Error: {e}")
28
- return None
 
29
 
30
  def getSystemPrompt():
31
- return text #TODO: System Prompt builder
 
32
 
33
  def getConvId():
34
  u = uuid.uuid4().bytes[:12]
35
  return base64.urlsafe_b64encode(u).rstrip(b'=').decode()
36
 
 
 
 
 
 
 
 
 
 
 
 
37
  @app.post("/new_conversation")
38
- async def handleNewConv(new_conv: NewConv, token: str = Cookie(None)):
39
  a = time.time()
40
- user_convs = sessions.get(token)
41
- if not user_convs:
42
- return "Error" # TODO: Error implementation
43
  history = [
44
  types.Content(role='model', parts=[types.Part(text=getSystemPrompt())]),
45
  types.Content(role='user', parts=[types.Part(text=new_conv.prompt)])
46
  ]
47
- text = gen(history)
48
  if text:
49
  conv_id = getConvId()
50
  raw_history = [["model", getSystemPrompt()]]
51
  raw_history.append(["user", new_conv.prompt])
52
  raw_history.append(["model", text])
53
- user_convs[conv_id] = raw_history
54
- return text
55
-
56
- return None # TODO: Error implementation
57
-
58
-
59
- @app.post("/new_conv")
60
- async def handleNewConv(new_conv: NewConv):
61
- a = time.time()
62
-
63
- return {'text': str(a), 'conv_id': getConvId()} # TODO: Error implementation
64
-
65
-
66
- @app.post("/gen_resp")
67
- async def handleNewConv(new_conv: NewConv):
68
- a = time.time()
69
-
70
- return {'text': str(a)} # TODO: Error implementation
71
 
72
 
73
  @app.post("/response")
74
- async def handleChat(chat_request: ChatRequest, token: str = Cookie(None)):
75
  a= time.time()
76
  conv_id = chat_request.conv_id
77
- # Check if token and conversation exist
78
- user_convs = sessions.get(token)
79
- if not user_convs or conv_id not in user_convs:
80
- return "Please stop. Just refresh the page." # TODO: Error implementation
81
  # Prepare history
82
- raw_history = user_convs[conv_id]
83
  history = [
84
  types.Content(role=k[0], parts=[types.Part(text=k[1])])
85
  for k in raw_history
86
  ]
87
  history.append(types.Content(role='user', parts=[types.Part(text=chat_request.prompt)]))
88
  # Generate response
89
- text = gen(history)
90
  if text:
91
  raw_history.append(["user", chat_request.prompt])
92
  raw_history.append(["model", text])
93
- # Optional: Update history with new response here
94
- return text
95
-
96
- return None # TODO: Error implementation
97
-
98
-
99
-
100
- if __name__ == "__main__":
101
- import uvicorn
102
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ from fastapi import FastAPI, Depends, HTTPException
2
+ from fastapi.security import HTTPAuthorizationCredentials
3
+ from pydantic import BaseModel
4
+ import time
5
+ import os
6
+ import base64
7
+ import uuid
8
  from google import genai
9
  from google.genai import types
10
+ from typing import List
11
+ import firebase_admin
12
+ from firebase_admin import credentials, auth
13
+
14
+ certificate = {
15
+ "type": "service_account",
16
+ "project_id": os.environ.get('project'),
17
+ "private_key_id": os.environ.get('key_id'),
18
+ "private_key": os.environ.get('private_key'),
19
+ "client_email": os.environ.get('client_email'),
20
+ "client_id": os.environ.get('client_id'),
21
+ "auth_uri": "https://accounts.google.com/o/oauth2/auth",
22
+ "token_uri": "https://oauth2.googleapis.com/token",
23
+ "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
24
+ "client_x509_cert_url": os.environ.get('cert_url'),
25
+ "universe_domain": "googleapis.com"
26
+ }
27
+
28
+ cred = credentials.Certificate(certificate)
29
+ firebase_admin.initialize_app(cred)
30
+
31
+
32
+ value = os.environ.get('YOUR_ENV_KEY')
33
 
34
  app = FastAPI()
35
 
36
 
37
  Tokens = []
38
+ user = []
39
+ # In-memory storage
40
+ sessions = {} # Format: {uid: {conv_id: [history_list]}}
41
+
42
+
43
+ user_details = [] #[[name, gender, nationality, model name, model gender]]
44
+ user_inst = [] #[[system prompt, About user, demanded tone, custum instruction]]
45
+
46
+
47
+ Api_key = os.getenv('API_KEY')
48
+ System_instruction = '''**System Prompt for a Programmer-Oriented Coding Assistant:**\n\n> You are a highly focused, fast, and expert-level coding assistant built for professional programmers.\n> Your primary role is **to assist with code writing, debugging, refactoring, optimization, and architecture**.\n> Avoid unnecessary explanations unless asked. Do not teach—**support the user like a senior pair programmer** who assumes context and skill. Prioritize clean, correct, and efficient code.\n\n> Always:\n> * Get straight to the point.\n> * Suggest the most practical and scalable solution.\n> * Respond with complete code blocks when needed.\n> * Use strong defaults and modern conventions.\n> * Assume the user knows what they're doing.\n> * Think ahead: anticipate potential pitfalls or better approaches.\n> * Give fast, minimal answers when asked for quick help.\n\n> Only elaborate if specifically requested (e.g., “explain,” “why,” “teach,” “verbose”)'''
49
+
50
+ client = genai.Client(api_key=Api_key)
51
+
52
 
53
  class ChatRequest(BaseModel):
54
  prompt: str
 
57
  class NewConv(BaseModel):
58
  prompt: str
59
 
60
+ class VerifyRequest(BaseModel):
61
+ uid: str
62
+ idToken: str
63
+
64
+
65
+ @app.post("/api/verify")
66
+ def check_token(data: VerifyRequest):
67
+ decoded_token = auth.verify_id_token(data.idToken)
68
+ uid = decoded_token['uid']
69
+
70
+ if decoded_token['uid'] == data.uid and decoded_token['email_verified']:
71
+ new_token = base64.urlsafe_b64encode(uuid.uuid4().bytes).rstrip(b'=').decode()
72
+
73
+ if uid not in user:
74
+ user.append(uid)
75
+ Tokens.append(new_token)
76
+ sessions[uid] = {}
77
+ else:
78
+ idx = user.index(uid)
79
+ Tokens[idx] = new_token
80
+ else:
81
+ raise HTTPException(status_code=401, detail="Invalid token. Verification failed.")
82
+
83
+
84
+ def call_gemini(history: List[types.Content]):
85
  try:
86
+ response = client.models.generate_content(
87
+ model="gemma-3-4b-it",
88
+ contents=history
89
+ )
90
  return response.text
91
  except Exception as e:
92
+ print(f"GenAI Error: {e}")
93
+ raise HTTPException(status_code=500, detail="AI Generation Failed")
94
+
95
 
96
  def getSystemPrompt():
97
+ text = ''
98
+ return text #TODO: System Prompt builder later
99
 
100
  def getConvId():
101
  u = uuid.uuid4().bytes[:12]
102
  return base64.urlsafe_b64encode(u).rstrip(b'=').decode()
103
 
104
+
105
+ def verify_access_token(auth: HTTPAuthorizationCredentials = Depends(security)):
106
+ token = auth.credentials
107
+ if token not in Tokens:
108
+ raise HTTPException(
109
+ status_code=401,
110
+ detail="Invalid or expired session token. Please login again."
111
+ )
112
+ return token
113
+
114
+
115
  @app.post("/new_conversation")
116
+ async def handleNewConv(new_conv: NewConv, token: str = Depends(verify_access_token)):
117
  a = time.time()
118
+ i = Tokens.index(token)
119
+ convs = sessions.get(user[i])
 
120
  history = [
121
  types.Content(role='model', parts=[types.Part(text=getSystemPrompt())]),
122
  types.Content(role='user', parts=[types.Part(text=new_conv.prompt)])
123
  ]
124
+ text = call_gemini(history)
125
  if text:
126
  conv_id = getConvId()
127
  raw_history = [["model", getSystemPrompt()]]
128
  raw_history.append(["user", new_conv.prompt])
129
  raw_history.append(["model", text])
130
+ convs[conv_id] = raw_history
131
+ return {"title": new_conv.prompt, "text": text, "conv_id": conv_id}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
 
134
  @app.post("/response")
135
+ async def handleChat(chat_request: ChatRequest, token: str = Depends(verify_access_token)):
136
  a= time.time()
137
  conv_id = chat_request.conv_id
138
+ i = Tokens.index(token)
139
+ convs = sessions.get(user[i])
140
+ if conv_id not in convs:
141
+ raise HTTPException(status_code=404, detail="Conversation not found")
142
  # Prepare history
143
+ raw_history = convs[conv_id]
144
  history = [
145
  types.Content(role=k[0], parts=[types.Part(text=k[1])])
146
  for k in raw_history
147
  ]
148
  history.append(types.Content(role='user', parts=[types.Part(text=chat_request.prompt)]))
149
  # Generate response
150
+ text = call_gemini(history)
151
  if text:
152
  raw_history.append(["user", chat_request.prompt])
153
  raw_history.append(["model", text])
154
+ return {"text": text}