Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ from pydantic import BaseModel
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
import re
|
| 6 |
-
import
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
@@ -19,17 +19,15 @@ class GeminiAPI:
|
|
| 19 |
self.session.cookies.update(self.cookies)
|
| 20 |
|
| 21 |
self.headers = {
|
| 22 |
-
'User-Agent': 'Mozilla/5.0 (
|
| 23 |
-
'Content-Type': 'application/
|
| 24 |
-
'
|
| 25 |
-
'
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
-
self.snlm0e = None
|
| 29 |
-
self.conversation_id = None
|
| 30 |
-
self.response_id = None
|
| 31 |
-
self.choice_id = None
|
| 32 |
-
|
| 33 |
def _parse_cookies(self, cookies_input):
|
| 34 |
cookies = {}
|
| 35 |
|
|
@@ -52,87 +50,64 @@ class GeminiAPI:
|
|
| 52 |
|
| 53 |
return cookies
|
| 54 |
|
| 55 |
-
def get_snlm0e_token(self):
|
| 56 |
-
try:
|
| 57 |
-
response = self.session.get(
|
| 58 |
-
'https://gemini.google.com/',
|
| 59 |
-
headers=self.headers,
|
| 60 |
-
timeout=10
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
if 'Sign in' in response.text or 'Login' in response.text:
|
| 64 |
-
return False, "Cookie tidak valid atau expired"
|
| 65 |
-
|
| 66 |
-
match = re.search(r'"SNlM0e":"(.*?)"', response.text)
|
| 67 |
-
if match:
|
| 68 |
-
self.snlm0e = match.group(1)
|
| 69 |
-
return True, "Token berhasil didapat"
|
| 70 |
-
|
| 71 |
-
return False, "Token tidak ditemukan"
|
| 72 |
-
|
| 73 |
-
except Exception as e:
|
| 74 |
-
return False, str(e)
|
| 75 |
-
|
| 76 |
def send_message(self, prompt, chat_id=None):
|
| 77 |
-
|
| 78 |
-
success, msg = self.get_snlm0e_token()
|
| 79 |
-
if not success:
|
| 80 |
-
return None, msg
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
[prompt],
|
| 91 |
None,
|
| 92 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
]
|
| 94 |
|
| 95 |
-
data = {
|
| 96 |
-
'f.req': json.dumps([None, json.dumps(message_struct)]),
|
| 97 |
-
'at': self.snlm0e,
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
try:
|
| 101 |
response = self.session.post(
|
| 102 |
url,
|
| 103 |
headers=self.headers,
|
| 104 |
-
|
| 105 |
timeout=30
|
| 106 |
)
|
| 107 |
|
| 108 |
if response.status_code != 200:
|
| 109 |
return None, f"HTTP Error: {response.status_code}"
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
if response_data and len(response_data) > 4:
|
| 121 |
-
text_response = response_data[4][0][1][0]
|
| 122 |
-
|
| 123 |
-
if len(response_data) > 1:
|
| 124 |
-
self.conversation_id = response_data[1][0]
|
| 125 |
-
self.response_id = response_data[1][1]
|
| 126 |
-
self.choice_id = response_data[4][0][0]
|
| 127 |
-
|
| 128 |
-
return {
|
| 129 |
-
'response': text_response,
|
| 130 |
-
'chat_id': self.conversation_id,
|
| 131 |
-
'response_id': self.response_id,
|
| 132 |
-
'choice_id': self.choice_id
|
| 133 |
-
}, None
|
| 134 |
-
except Exception as e:
|
| 135 |
-
continue
|
| 136 |
|
| 137 |
return None, "Tidak bisa parse response"
|
| 138 |
|
|
@@ -157,11 +132,12 @@ async def chat(request: ChatRequest):
|
|
| 157 |
async def root():
|
| 158 |
return {
|
| 159 |
"message": "Gemini API",
|
|
|
|
| 160 |
"endpoints": {
|
| 161 |
"POST /chat": {
|
| 162 |
"cookies": "string (JSON array atau cookie string)",
|
| 163 |
"prompt": "string",
|
| 164 |
-
"chat_id": "string (optional
|
| 165 |
}
|
| 166 |
}
|
| 167 |
}
|
|
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
import re
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
| 19 |
self.session.cookies.update(self.cookies)
|
| 20 |
|
| 21 |
self.headers = {
|
| 22 |
+
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36',
|
| 23 |
+
'Content-Type': 'application/json+protobuf',
|
| 24 |
+
'Accept': '*/*',
|
| 25 |
+
'Origin': 'https://aistudio.google.com',
|
| 26 |
+
'Referer': 'https://aistudio.google.com/',
|
| 27 |
+
'X-Goog-Api-Key': 'AIzaSyD0fP816MBRBSkj20Q04XbjsIcI0jGI0WOs',
|
| 28 |
+
'X-Client-Data': 'CJGVywE=',
|
| 29 |
}
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def _parse_cookies(self, cookies_input):
|
| 32 |
cookies = {}
|
| 33 |
|
|
|
|
| 50 |
|
| 51 |
return cookies
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
def send_message(self, prompt, chat_id=None):
|
| 54 |
+
url = 'https://alkalimakersuite-pa.clients6.google.com/$rpc/google.internal.alkali.applications.makersuite.v1.MakerSuiteService/GenerateContent'
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
payload = [
|
| 57 |
+
"models/gemini-3-flash-preview",
|
| 58 |
+
[[[[None, prompt]], "user"]],
|
| 59 |
+
None,
|
| 60 |
+
0,
|
| 61 |
+
"models/gemini-3-flash-preview",
|
| 62 |
+
[None, None, None, 65536, 1, 0.95, 64, None, None, None, None, None, None, None],
|
| 63 |
+
"!GBu1G0PNAAZ19fabc_VClWQf4_wu6go7ADQBEArZ1NrXSqgcKEzmXhn1r70At0ogWMU1G6bOhL3",
|
|
|
|
| 64 |
None,
|
| 65 |
+
[[None, None, [[]]],
|
| 66 |
+
None,
|
| 67 |
+
None,
|
| 68 |
+
None,
|
| 69 |
+
None,
|
| 70 |
+
None,
|
| 71 |
+
None,
|
| 72 |
+
None,
|
| 73 |
+
None,
|
| 74 |
+
[[None, None, "Asia/Makassar"]]
|
| 75 |
]
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
try:
|
| 78 |
response = self.session.post(
|
| 79 |
url,
|
| 80 |
headers=self.headers,
|
| 81 |
+
json=payload,
|
| 82 |
timeout=30
|
| 83 |
)
|
| 84 |
|
| 85 |
if response.status_code != 200:
|
| 86 |
return None, f"HTTP Error: {response.status_code}"
|
| 87 |
|
| 88 |
+
data = response.json()
|
| 89 |
+
|
| 90 |
+
text_response = ""
|
| 91 |
+
|
| 92 |
+
if isinstance(data, list) and len(data) > 0:
|
| 93 |
+
for item in data:
|
| 94 |
+
if isinstance(item, list) and len(item) > 0:
|
| 95 |
+
for subitem in item:
|
| 96 |
+
if isinstance(subitem, list) and len(subitem) > 0:
|
| 97 |
+
for content in subitem:
|
| 98 |
+
if isinstance(content, list) and len(content) > 0:
|
| 99 |
+
for text_item in content:
|
| 100 |
+
if isinstance(text_item, list) and len(text_item) > 0:
|
| 101 |
+
if text_item[0] is None and len(text_item) > 1:
|
| 102 |
+
if isinstance(text_item[1], str):
|
| 103 |
+
text_response += text_item[1]
|
| 104 |
|
| 105 |
+
if text_response:
|
| 106 |
+
return {
|
| 107 |
+
'response': text_response.strip(),
|
| 108 |
+
'chat_id': chat_id,
|
| 109 |
+
'timestamp': int(time.time())
|
| 110 |
+
}, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
return None, "Tidak bisa parse response"
|
| 113 |
|
|
|
|
| 132 |
async def root():
|
| 133 |
return {
|
| 134 |
"message": "Gemini API",
|
| 135 |
+
"version": "2.0",
|
| 136 |
"endpoints": {
|
| 137 |
"POST /chat": {
|
| 138 |
"cookies": "string (JSON array atau cookie string)",
|
| 139 |
"prompt": "string",
|
| 140 |
+
"chat_id": "string (optional)"
|
| 141 |
}
|
| 142 |
}
|
| 143 |
}
|