Abu1998 commited on
Commit
7c86a1d
Β·
verified Β·
1 Parent(s): 932f7ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -9
app.py CHANGED
@@ -4,14 +4,11 @@ import requests
4
  from fastapi import FastAPI, Form
5
  from instagrapi import Client
6
  from PIL import Image
 
7
 
8
- # Initialize FastAPI app
9
  app = FastAPI()
10
-
11
- # Instagram client
12
  cl = Client()
13
 
14
- # Load Instagram credentials from HF secrets
15
  IG_USERNAME = os.getenv("USERNAME")
16
  IG_PASSWORD = os.getenv("PASSWORD")
17
 
@@ -24,7 +21,6 @@ def login():
24
  raise
25
 
26
  def fix_google_drive_url(url: str) -> str:
27
- """Convert Google Drive share link to direct-download link"""
28
  if "drive.google.com" in url:
29
  if "/d/" in url:
30
  file_id = url.split("/d/")[1].split("/")[0]
@@ -37,25 +33,21 @@ def upload_post(photo_url: str = Form(...), caption: str = Form(...)):
37
  if not cl.user_id:
38
  login()
39
 
40
- # Fix Google Drive links
41
  photo_url = fix_google_drive_url(photo_url)
42
 
43
  response = requests.get(photo_url, stream=True)
44
  if response.status_code != 200:
45
  return {"status": "error", "message": f"Invalid image URL: {response.status_code}"}
46
 
47
- # Save downloaded file
48
  tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
49
  tmp_file.write(response.content)
50
  tmp_file.close()
51
 
52
- # Validate image
53
  try:
54
  Image.open(tmp_file.name).verify()
55
  except Exception:
56
  return {"status": "error", "message": "Downloaded file is not a valid image."}
57
 
58
- # Upload to Instagram
59
  cl.photo_upload(tmp_file.name, caption)
60
  return {"status": "success", "message": "Photo uploaded successfully!"}
61
 
@@ -65,3 +57,7 @@ def upload_post(photo_url: str = Form(...), caption: str = Form(...)):
65
  @app.get("/")
66
  def root():
67
  return {"status": "ok", "message": "Instagram uploader is running πŸš€"}
 
 
 
 
 
4
  from fastapi import FastAPI, Form
5
  from instagrapi import Client
6
  from PIL import Image
7
+ import uvicorn
8
 
 
9
  app = FastAPI()
 
 
10
  cl = Client()
11
 
 
12
  IG_USERNAME = os.getenv("USERNAME")
13
  IG_PASSWORD = os.getenv("PASSWORD")
14
 
 
21
  raise
22
 
23
  def fix_google_drive_url(url: str) -> str:
 
24
  if "drive.google.com" in url:
25
  if "/d/" in url:
26
  file_id = url.split("/d/")[1].split("/")[0]
 
33
  if not cl.user_id:
34
  login()
35
 
 
36
  photo_url = fix_google_drive_url(photo_url)
37
 
38
  response = requests.get(photo_url, stream=True)
39
  if response.status_code != 200:
40
  return {"status": "error", "message": f"Invalid image URL: {response.status_code}"}
41
 
 
42
  tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
43
  tmp_file.write(response.content)
44
  tmp_file.close()
45
 
 
46
  try:
47
  Image.open(tmp_file.name).verify()
48
  except Exception:
49
  return {"status": "error", "message": "Downloaded file is not a valid image."}
50
 
 
51
  cl.photo_upload(tmp_file.name, caption)
52
  return {"status": "success", "message": "Photo uploaded successfully!"}
53
 
 
57
  @app.get("/")
58
  def root():
59
  return {"status": "ok", "message": "Instagram uploader is running πŸš€"}
60
+
61
+ # πŸ”Ή Add this so Hugging Face knows how to start it
62
+ if __name__ == "__main__":
63
+ uvicorn.run("app:app", host="0.0.0.0", port=7860)