Spaces:
Runtime error
Runtime error
changes
Browse files- app/gmail_auth.py +12 -15
- app/nodes/check_email_exist_node.py +2 -2
- authenticate_gmail.py +21 -21
app/gmail_auth.py
CHANGED
|
@@ -3,48 +3,45 @@ from pathlib import Path
|
|
| 3 |
from app.core.config import settings
|
| 4 |
|
| 5 |
# CREATE TEMP DIRECTORY
|
| 6 |
-
|
| 7 |
AUTH_DIR = Path("/tmp/google_auth")
|
| 8 |
AUTH_DIR.mkdir(parents=True, exist_ok=True)
|
| 9 |
|
| 10 |
# FULL FILE PATHS
|
| 11 |
-
|
| 12 |
CREDENTIALS_PATH = AUTH_DIR / "credentials.json"
|
| 13 |
TOKEN_PATH = AUTH_DIR / "token.json"
|
| 14 |
|
| 15 |
-
# CREATE FILES
|
| 16 |
-
|
| 17 |
-
if
|
| 18 |
CREDENTIALS_PATH.write_text(os.environ["GOOGLE_CREDENTIALS"])
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
if
|
| 21 |
TOKEN_PATH.write_text(os.environ["GOOGLE_TOKEN"])
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# GMAIL IMPORTS
|
| 24 |
-
|
| 25 |
from langchain_google_community.gmail.utils import (
|
| 26 |
build_resource_service,
|
| 27 |
get_gmail_credentials,
|
| 28 |
)
|
| 29 |
-
|
| 30 |
from langchain_google_community import GmailToolkit
|
| 31 |
|
| 32 |
-
# LOAD CREDENTIALS
|
| 33 |
-
|
| 34 |
credentials = get_gmail_credentials(
|
| 35 |
-
token_file=str(TOKEN_PATH),
|
| 36 |
scopes=["https://mail.google.com/"],
|
| 37 |
-
|
| 38 |
)
|
| 39 |
|
| 40 |
# BUILD API RESOURCE
|
| 41 |
-
|
| 42 |
api_resource = build_resource_service(
|
| 43 |
credentials=credentials
|
| 44 |
)
|
| 45 |
|
| 46 |
# TOOLKIT
|
| 47 |
-
|
| 48 |
gmail_toolkit = GmailToolkit(
|
| 49 |
api_resource=api_resource
|
| 50 |
-
)
|
|
|
|
| 3 |
from app.core.config import settings
|
| 4 |
|
| 5 |
# CREATE TEMP DIRECTORY
|
|
|
|
| 6 |
AUTH_DIR = Path("/tmp/google_auth")
|
| 7 |
AUTH_DIR.mkdir(parents=True, exist_ok=True)
|
| 8 |
|
| 9 |
# FULL FILE PATHS
|
|
|
|
| 10 |
CREDENTIALS_PATH = AUTH_DIR / "credentials.json"
|
| 11 |
TOKEN_PATH = AUTH_DIR / "token.json"
|
| 12 |
|
| 13 |
+
# FORCE CREATE/VERIFY FILES AT RUNTIME
|
| 14 |
+
# (Removed 'if not exists' check to ensure they always write correctly from HF Secrets)
|
| 15 |
+
if "GOOGLE_CREDENTIALS" in os.environ:
|
| 16 |
CREDENTIALS_PATH.write_text(os.environ["GOOGLE_CREDENTIALS"])
|
| 17 |
+
else:
|
| 18 |
+
raise ValueError("GOOGLE_CREDENTIALS secret is missing from environment variables!")
|
| 19 |
|
| 20 |
+
if "GOOGLE_TOKEN" in os.environ:
|
| 21 |
TOKEN_PATH.write_text(os.environ["GOOGLE_TOKEN"])
|
| 22 |
+
else:
|
| 23 |
+
raise ValueError("GOOGLE_TOKEN secret is missing from environment variables!")
|
| 24 |
|
| 25 |
# GMAIL IMPORTS
|
|
|
|
| 26 |
from langchain_google_community.gmail.utils import (
|
| 27 |
build_resource_service,
|
| 28 |
get_gmail_credentials,
|
| 29 |
)
|
|
|
|
| 30 |
from langchain_google_community import GmailToolkit
|
| 31 |
|
| 32 |
+
# LOAD CREDENTIALS - Fixed parameter name to 'client_secrets_file'
|
|
|
|
| 33 |
credentials = get_gmail_credentials(
|
| 34 |
+
token_file=str(TOKEN_PATH.resolve()),
|
| 35 |
scopes=["https://mail.google.com/"],
|
| 36 |
+
client_sercrets_file=str(CREDENTIALS_PATH.resolve()), # <-- Fixed typo here
|
| 37 |
)
|
| 38 |
|
| 39 |
# BUILD API RESOURCE
|
|
|
|
| 40 |
api_resource = build_resource_service(
|
| 41 |
credentials=credentials
|
| 42 |
)
|
| 43 |
|
| 44 |
# TOOLKIT
|
|
|
|
| 45 |
gmail_toolkit = GmailToolkit(
|
| 46 |
api_resource=api_resource
|
| 47 |
+
)
|
app/nodes/check_email_exist_node.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
|
| 2 |
from app.state.state import EmailAgentState
|
| 3 |
from langchain_google_community import GmailToolkit
|
|
|
|
| 4 |
|
| 5 |
def check_previous_email_exist_node(state: EmailAgentState):
|
| 6 |
# Search for previous interactions with this sender
|
| 7 |
-
|
| 8 |
-
search_tool = [t for t in toolkit.get_tools() if t.name == "search_gmail"][0]
|
| 9 |
results = search_tool.invoke(f"from:{state['sender_email_id']}")
|
| 10 |
|
| 11 |
if len(results) == 0:
|
|
|
|
| 1 |
|
| 2 |
from app.state.state import EmailAgentState
|
| 3 |
from langchain_google_community import GmailToolkit
|
| 4 |
+
from app.gmail_auth import gmail_toolkit ,api_resource
|
| 5 |
|
| 6 |
def check_previous_email_exist_node(state: EmailAgentState):
|
| 7 |
# Search for previous interactions with this sender
|
| 8 |
+
search_tool = [t for t in gmail_toolkit.get_tools() if t.name == "search_gmail"][0]
|
|
|
|
| 9 |
results = search_tool.invoke(f"from:{state['sender_email_id']}")
|
| 10 |
|
| 11 |
if len(results) == 0:
|
authenticate_gmail.py
CHANGED
|
@@ -1,29 +1,29 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from google_auth_oauthlib.flow import InstalledAppFlow
|
| 3 |
|
| 4 |
-
# Gmail API scope
|
| 5 |
-
SCOPES = [
|
| 6 |
-
|
| 7 |
-
]
|
| 8 |
|
| 9 |
-
def run_authentication():
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
-
if __name__ == "__main__":
|
| 29 |
-
|
|
|
|
| 1 |
+
# import os
|
| 2 |
+
# from google_auth_oauthlib.flow import InstalledAppFlow
|
| 3 |
|
| 4 |
+
# # Gmail API scope
|
| 5 |
+
# SCOPES = [
|
| 6 |
+
# "https://mail.google.com/"
|
| 7 |
+
# ]
|
| 8 |
|
| 9 |
+
# def run_authentication():
|
| 10 |
+
# print("Starting Google OAuth Authentication Flow...")
|
| 11 |
|
| 12 |
+
# if not os.path.exists("credentials.json"):
|
| 13 |
+
# print("ERROR: Please place your 'credentials.json' file in this folder first!")
|
| 14 |
+
# return
|
| 15 |
|
| 16 |
+
# flow = InstalledAppFlow.from_client_secrets_file(
|
| 17 |
+
# "credentials.json",
|
| 18 |
+
# SCOPES
|
| 19 |
+
# )
|
| 20 |
|
| 21 |
+
# creds = flow.run_local_server(port=8080)
|
| 22 |
|
| 23 |
+
# with open("token.json", "w") as token_file:
|
| 24 |
+
# token_file.write(creds.to_json())
|
| 25 |
|
| 26 |
+
# print("\nSUCCESS: 'token.json' has been created successfully!")
|
| 27 |
|
| 28 |
+
# if __name__ == "__main__":
|
| 29 |
+
# run_authentication()
|