ffxbot commited on
Commit
0897526
·
verified ·
1 Parent(s): 0469565

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +6 -17
main.py CHANGED
@@ -6,7 +6,7 @@ from io import BytesIO
6
 
7
  app = FastAPI()
8
 
9
- # গুগল-এর একটি ফাস্ট এবং পাওয়ারফুল ভিশন এআই মডেল
10
  classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
11
 
12
  @app.get("/")
@@ -16,31 +16,20 @@ def read_root():
16
  @app.get("/analyze")
17
  def analyze(url: str):
18
  try:
19
- # টিকটকেম্বনেইল সরাসরি করস্ক্যান কর
20
- res = requests.get(url, timeout=10)
 
21
  img = Image.open(BytesIO(res.content)).convert("RGB")
22
 
23
  # AI Processing
24
  results = classifier(img)
25
-
26
- # Confidence Check (85% = 0.85)
27
  top_score = results[0]['score']
28
 
29
  if top_score >= 0.85:
30
- # যদি এআই প্রায় ১০০% নিশ্চিত হয়, তবে শুধু ১টি নাম দেবে
31
- return {
32
- "success": True,
33
- "confidence": f"High ({int(top_score*100)}%)",
34
- "names": [results[0]['label'].title()]
35
- }
36
  else:
37
- # যদি এআই পুরোপুরি নিশ্চিত না হয়, তবে সম্ভাব্য সেরা ৩টি নাম দেবে
38
  top_3 = [r['label'].title() for r in results[:3]]
39
- return {
40
- "success": True,
41
- "confidence": "Low",
42
- "names": top_3
43
- }
44
 
45
  except Exception as e:
46
  return {"success": False, "error": str(e)}
 
6
 
7
  app = FastAPI()
8
 
9
+ # গুগল-এর ভিশন এআই মডেল
10
  classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
11
 
12
  @app.get("/")
 
16
  @app.get("/analyze")
17
  def analyze(url: str):
18
  try:
19
+ # ব্রাউজারেজে রিকোয়েস্ট ঠানো টিটক বলক কর
20
+ headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64 AppleWebKit/537.36)"}
21
+ res = requests.get(url, headers=headers, timeout=15)
22
  img = Image.open(BytesIO(res.content)).convert("RGB")
23
 
24
  # AI Processing
25
  results = classifier(img)
 
 
26
  top_score = results[0]['score']
27
 
28
  if top_score >= 0.85:
29
+ return {"success": True, "confidence": f"High ({int(top_score*100)}%)", "names": [results[0]['label'].title()]}
 
 
 
 
 
30
  else:
 
31
  top_3 = [r['label'].title() for r in results[:3]]
32
+ return {"success": True, "confidence": "Low", "names": top_3}
 
 
 
 
33
 
34
  except Exception as e:
35
  return {"success": False, "error": str(e)}