PluginLiveInterns commited on
Commit
2968573
·
1 Parent(s): 0cbed61

Add application file4

Browse files
Files changed (1) hide show
  1. app.py +41 -12
app.py CHANGED
@@ -4,31 +4,56 @@ import json
4
  import time
5
  import streamlit as st
6
  import base64
 
7
  from io import BytesIO
8
  from dotenv import load_dotenv
9
- # Remove Selenium imports
10
- # from selenium import webdriver
11
- # from selenium.webdriver.chrome.options import Options
12
- # from selenium.webdriver.common.by import By
13
- # from selenium.webdriver.common.keys import Keys
14
- # from selenium.webdriver.support.ui import WebDriverWait
15
- # from selenium.webdriver.support import expected_conditions as EC
16
  from google import genai
17
- # Add Playwright imports
18
  from playwright.sync_api import sync_playwright
19
  import platform
 
20
 
21
  # Load environment variables from .env
22
  load_dotenv()
23
  GEMINI_API_KEY = 'AIzaSyAOK9vRTSRQzd22B2gmbiuIePbZTDyaGYs'
24
 
25
- # --- Playwright Browser Setup ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  def get_browser():
28
  """Get a Playwright browser instance that works in Hugging Face Spaces"""
29
  try:
 
 
 
 
 
 
30
  st.info("Initializing Playwright browser...")
31
- # This will download the browser if needed
32
  playwright_instance = sync_playwright().start()
33
  browser = playwright_instance.chromium.launch(
34
  headless=True,
@@ -434,13 +459,18 @@ def login_to_google(browser_data, email, password):
434
  return False
435
 
436
  # --- Streamlit App ---
437
-
438
  st.title("Google Form Auto Filler with Gemini")
439
  st.write("""
440
  This app uses a headless browser to help you fill Google Forms automatically with AI-generated answers.
441
  You'll be able to see screenshots of what's happening in the browser as it progresses.
442
  """)
443
 
 
 
 
 
 
 
444
  # Initialize session state variables
445
  if "browser_data" not in st.session_state:
446
  st.session_state.browser_data = None
@@ -450,7 +480,6 @@ if "form_filled" not in st.session_state:
450
  st.session_state.form_filled = False
451
  if "screenshot" not in st.session_state:
452
  st.session_state.screenshot = None
453
-
454
  # Step 1: Login to Google Account
455
  st.header("Step 1: Login to Google Account")
456
 
 
4
  import time
5
  import streamlit as st
6
  import base64
7
+ import subprocess # Add subprocess import
8
  from io import BytesIO
9
  from dotenv import load_dotenv
 
 
 
 
 
 
 
10
  from google import genai
 
11
  from playwright.sync_api import sync_playwright
12
  import platform
13
+ import sys
14
 
15
  # Load environment variables from .env
16
  load_dotenv()
17
  GEMINI_API_KEY = 'AIzaSyAOK9vRTSRQzd22B2gmbiuIePbZTDyaGYs'
18
 
19
+ # --- Playwright Setup ---
20
+
21
+ if "PLAYWRIGHT_BROWSERS_INSTALLED" not in os.environ:
22
+ st.info("Installing Playwright browsers...")
23
+ os.system("playwright install chromium")
24
+ os.environ["PLAYWRIGHT_BROWSERS_INSTALLED"] = "1"
25
+ st.success("Browser installation complete!")
26
+
27
+ def install_playwright_browsers():
28
+ """Install Playwright browsers programmatically"""
29
+ try:
30
+ st.info("Installing Playwright browsers...")
31
+ result = subprocess.run(
32
+ [sys.executable, "-m", "playwright", "install", "chromium"],
33
+ capture_output=True,
34
+ text=True,
35
+ check=True
36
+ )
37
+ st.success("Playwright browsers installed successfully!")
38
+ return True
39
+ except subprocess.CalledProcessError as e:
40
+ st.error(f"Failed to install Playwright browsers: {e}")
41
+ st.error(f"Error output: {e.stderr}")
42
+ return False
43
+ except Exception as e:
44
+ st.error(f"Unexpected error installing browsers: {e}")
45
+ return False
46
 
47
  def get_browser():
48
  """Get a Playwright browser instance that works in Hugging Face Spaces"""
49
  try:
50
+ # Make sure browsers are installed first
51
+ if "playwright_browsers_installed" not in st.session_state:
52
+ if not install_playwright_browsers():
53
+ raise Exception("Failed to install browser binaries")
54
+ st.session_state.playwright_browsers_installed = True
55
+
56
  st.info("Initializing Playwright browser...")
 
57
  playwright_instance = sync_playwright().start()
58
  browser = playwright_instance.chromium.launch(
59
  headless=True,
 
459
  return False
460
 
461
  # --- Streamlit App ---
 
462
  st.title("Google Form Auto Filler with Gemini")
463
  st.write("""
464
  This app uses a headless browser to help you fill Google Forms automatically with AI-generated answers.
465
  You'll be able to see screenshots of what's happening in the browser as it progresses.
466
  """)
467
 
468
+ # Install Playwright browsers when app starts (if not already installed)
469
+ if "playwright_browsers_installed" not in st.session_state:
470
+ with st.spinner("Setting up browser environment (this might take a few minutes the first time)..."):
471
+ install_playwright_browsers()
472
+ st.session_state.playwright_browsers_installed = True
473
+
474
  # Initialize session state variables
475
  if "browser_data" not in st.session_state:
476
  st.session_state.browser_data = None
 
480
  st.session_state.form_filled = False
481
  if "screenshot" not in st.session_state:
482
  st.session_state.screenshot = None
 
483
  # Step 1: Login to Google Account
484
  st.header("Step 1: Login to Google Account")
485