Muhammad Saqib
commited on
Update modules/app.py
Browse files- modules/app.py +22 -10
modules/app.py
CHANGED
@@ -2,16 +2,25 @@ from fastapi import FastAPI, HTTPException, Request, Depends
|
|
2 |
import aiohttp
|
3 |
import base64
|
4 |
import io
|
5 |
-
import
|
6 |
import random
|
7 |
import string
|
8 |
import json
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
async def verify_user_agent(request: Request):
|
13 |
user_agent = request.headers.get('User-Agent', None)
|
14 |
-
if user_agent !=
|
15 |
raise HTTPException(status_code=403, detail="Access denied")
|
16 |
return True
|
17 |
|
@@ -40,9 +49,9 @@ async def whisper(request: Request):
|
|
40 |
'Connection': 'keep-alive',
|
41 |
'Content-Type': 'application/json',
|
42 |
'DNT': '1',
|
43 |
-
'Origin':
|
44 |
'Pragma': 'no-cache',
|
45 |
-
'Referer': '
|
46 |
'Sec-Fetch-Dest': 'empty',
|
47 |
'Sec-Fetch-Mode': 'cors',
|
48 |
'Sec-Fetch-Site': 'same-site',
|
@@ -65,7 +74,7 @@ async def whisper(request: Request):
|
|
65 |
json_data = '{"audio": "' + audio_base64 + '"}'
|
66 |
|
67 |
# Post request to the API
|
68 |
-
async with session.post('
|
69 |
if post_resp.status != 200:
|
70 |
return f"API request failed: {post_resp.status}"
|
71 |
return await post_resp.json()
|
@@ -106,10 +115,10 @@ async def img2location(request: Request):
|
|
106 |
'accept-language': 'en-US,en;q=0.9',
|
107 |
'cache-control': 'no-cache',
|
108 |
'dnt': '1',
|
109 |
-
'origin':
|
110 |
'pragma': 'no-cache',
|
111 |
'priority': 'u=1, i',
|
112 |
-
'referer': '
|
113 |
'sec-ch-ua': '"Chromium";v="124", "Microsoft Edge";v="124", "Not-A.Brand";v="99"',
|
114 |
'sec-ch-ua-mobile': '?0',
|
115 |
'sec-ch-ua-platform': '"Windows"',
|
@@ -131,7 +140,7 @@ async def img2location(request: Request):
|
|
131 |
data.add_field('image', io.BytesIO(image_data), filename="image.png", content_type='image/png')
|
132 |
|
133 |
# Sending the POST request
|
134 |
-
async with session.post(
|
135 |
if response.status != 200:
|
136 |
return f"Failed to upload image: HTTP {response.status}"
|
137 |
json_response = await response.json()
|
@@ -145,6 +154,9 @@ async def img2location(request: Request):
|
|
145 |
google_maps = f"https://www.google.com/maps/search/?api=1&query={latitude},{longitude}"
|
146 |
|
147 |
json_response['message'] += f"\n\nView on Google Maps: {google_maps}"
|
|
|
|
|
|
|
148 |
|
149 |
return json_response
|
150 |
|
@@ -202,14 +214,14 @@ async def pixart_sigma(request: Request):
|
|
202 |
}
|
203 |
|
204 |
async with aiohttp.ClientSession() as session:
|
205 |
-
async with session.post('
|
206 |
print(response.text)
|
207 |
|
208 |
params = {
|
209 |
'session_hash': hash,
|
210 |
}
|
211 |
|
212 |
-
async with session.get('
|
213 |
async for line in response.content:
|
214 |
try:
|
215 |
if line:
|
|
|
2 |
import aiohttp
|
3 |
import base64
|
4 |
import io
|
5 |
+
import os
|
6 |
import random
|
7 |
import string
|
8 |
import json
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
12 |
+
whisper_origin = os.getenv("WHISPER_ORIGIN")
|
13 |
+
whisper_base_url = os.getenv("WHISPER_BASE_URL")
|
14 |
+
img2location_name = os.getenv("IMG2LOCATION_NAME")
|
15 |
+
img2location_origin = os.getenv("IMG2LOCATION_ORIGIN")
|
16 |
+
img2location_base_url = os.getenv("IMG2LOCATION_BASE_URL")
|
17 |
+
pixart_sigma_base_url = os.getenv("PIXART_SIGMA_BASE_URL")
|
18 |
+
allowed_user_agent = os.getenv("ALLOWED_USER_AGENT")
|
19 |
+
|
20 |
+
|
21 |
async def verify_user_agent(request: Request):
|
22 |
user_agent = request.headers.get('User-Agent', None)
|
23 |
+
if user_agent != allowed_user_agent:
|
24 |
raise HTTPException(status_code=403, detail="Access denied")
|
25 |
return True
|
26 |
|
|
|
49 |
'Connection': 'keep-alive',
|
50 |
'Content-Type': 'application/json',
|
51 |
'DNT': '1',
|
52 |
+
'Origin': whisper_origin,
|
53 |
'Pragma': 'no-cache',
|
54 |
+
'Referer': f'{whisper_origin}/',
|
55 |
'Sec-Fetch-Dest': 'empty',
|
56 |
'Sec-Fetch-Mode': 'cors',
|
57 |
'Sec-Fetch-Site': 'same-site',
|
|
|
74 |
json_data = '{"audio": "' + audio_base64 + '"}'
|
75 |
|
76 |
# Post request to the API
|
77 |
+
async with session.post(f'{whisper_base_url}/v1/inference/openai/whisper-large', headers=headers, data=json_data) as post_resp:
|
78 |
if post_resp.status != 200:
|
79 |
return f"API request failed: {post_resp.status}"
|
80 |
return await post_resp.json()
|
|
|
115 |
'accept-language': 'en-US,en;q=0.9',
|
116 |
'cache-control': 'no-cache',
|
117 |
'dnt': '1',
|
118 |
+
'origin': img2location_origin,
|
119 |
'pragma': 'no-cache',
|
120 |
'priority': 'u=1, i',
|
121 |
+
'referer': f'{img2location_origin}/',
|
122 |
'sec-ch-ua': '"Chromium";v="124", "Microsoft Edge";v="124", "Not-A.Brand";v="99"',
|
123 |
'sec-ch-ua-mobile': '?0',
|
124 |
'sec-ch-ua-platform': '"Windows"',
|
|
|
140 |
data.add_field('image', io.BytesIO(image_data), filename="image.png", content_type='image/png')
|
141 |
|
142 |
# Sending the POST request
|
143 |
+
async with session.post(img2location_base_url, headers=headers, data=data) as response:
|
144 |
if response.status != 200:
|
145 |
return f"Failed to upload image: HTTP {response.status}"
|
146 |
json_response = await response.json()
|
|
|
154 |
google_maps = f"https://www.google.com/maps/search/?api=1&query={latitude},{longitude}"
|
155 |
|
156 |
json_response['message'] += f"\n\nView on Google Maps: {google_maps}"
|
157 |
+
|
158 |
+
if img2location_name in json_response['message'].lower():
|
159 |
+
raise HTTPException(status_code=400, detail="We are not allowed to process this image. Please try another one.")
|
160 |
|
161 |
return json_response
|
162 |
|
|
|
214 |
}
|
215 |
|
216 |
async with aiohttp.ClientSession() as session:
|
217 |
+
async with session.post(f'{pixart_sigma_base_url}/queue/join', params=params, headers=headers, json=json_data) as response:
|
218 |
print(response.text)
|
219 |
|
220 |
params = {
|
221 |
'session_hash': hash,
|
222 |
}
|
223 |
|
224 |
+
async with session.get(f'{pixart_sigma_base_url}/queue/data', params=params, headers=headers) as response:
|
225 |
async for line in response.content:
|
226 |
try:
|
227 |
if line:
|