Spaces:
Sleeping
Sleeping
Commit ·
233222c
0
Parent(s):
for pushin
Browse files- Dockerfile +18 -0
- GlacialIndifference-Bold.otf +0 -0
- Primary logo. (1).png +0 -0
- requirements.txt +14 -0
- src/__init__.py +49 -0
- src/__pycache__/__init__.cpython-311.pyc +0 -0
- src/__pycache__/__init__.cpython-312.pyc +0 -0
- src/__pycache__/constants.cpython-311.pyc +0 -0
- src/__pycache__/constants.cpython-312.pyc +0 -0
- src/__pycache__/main.cpython-311.pyc +0 -0
- src/__pycache__/models.cpython-311.pyc +0 -0
- src/__pycache__/models.cpython-312.pyc +0 -0
- src/__pycache__/questions.cpython-311.pyc +0 -0
- src/__pycache__/questions.cpython-312.pyc +0 -0
- src/__pycache__/testllmscrape.cpython-311-pytest-8.3.4.pyc +0 -0
- src/__pycache__/utils.cpython-311.pyc +0 -0
- src/__pycache__/utils.cpython-312.pyc +0 -0
- src/constants.py +58 -0
- src/main.py +395 -0
- src/testllmscrape.py +80 -0
- src/utils/__init__.py +97 -0
- src/utils/__pycache__/__init__.cpython-311.pyc +0 -0
- src/utils/__pycache__/api_utils.cpython-311.pyc +0 -0
- src/utils/__pycache__/content_generation.cpython-311.pyc +0 -0
- src/utils/__pycache__/core.cpython-311.pyc +0 -0
- src/utils/__pycache__/database.cpython-311.pyc +0 -0
- src/utils/__pycache__/database_utils.cpython-311.pyc +0 -0
- src/utils/__pycache__/date_handler.cpython-311.pyc +0 -0
- src/utils/__pycache__/llm_utils.cpython-311.pyc +0 -0
- src/utils/__pycache__/partner_utils.cpython-311.pyc +0 -0
- src/utils/__pycache__/testllmscrape.cpython-311-pytest-8.3.4.pyc +0 -0
- src/utils/api_utils.py +189 -0
- src/utils/content_generation.py +198 -0
- src/utils/core.py +19 -0
- src/utils/database.py +75 -0
- src/utils/database_utils.py +51 -0
- src/utils/date_handler.py +148 -0
- src/utils/llm_utils.py +222 -0
- src/utils/partner_utils.py +290 -0
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use the official Python image
|
| 2 |
+
FROM python:3.10
|
| 3 |
+
|
| 4 |
+
# Set the working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy and install dependencies
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
+
|
| 11 |
+
# Copy the application files
|
| 12 |
+
COPY . .
|
| 13 |
+
|
| 14 |
+
# Expose the default Hugging Face Spaces port (7860)
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# Run the FastAPI application with Uvicorn
|
| 18 |
+
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
GlacialIndifference-Bold.otf
ADDED
|
Binary file (31.5 kB). View file
|
|
|
Primary logo. (1).png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
pydantic
|
| 3 |
+
python-dotenv
|
| 4 |
+
requests
|
| 5 |
+
langchain-core
|
| 6 |
+
langchain-google-genai
|
| 7 |
+
pytz
|
| 8 |
+
python-dateutil
|
| 9 |
+
pymysql
|
| 10 |
+
beautifulsoup4
|
| 11 |
+
uvicorn
|
| 12 |
+
urllib3
|
| 13 |
+
selenium>=4.11.0
|
| 14 |
+
webdriver-manager>=4.0.0
|
src/__init__.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .constants import DEFAULT_DRILL_INFO, CATEGORY_SUBCATEGORY_MAP
|
| 2 |
+
from .utils import (
|
| 3 |
+
get_llm,
|
| 4 |
+
DateHandler,
|
| 5 |
+
DatabaseHandler,
|
| 6 |
+
generate_drill_description,
|
| 7 |
+
generate_drill_overview_description,
|
| 8 |
+
generate_random_event_names,
|
| 9 |
+
generate_faq_answers,
|
| 10 |
+
infer_subcategory,
|
| 11 |
+
infer_purpose,
|
| 12 |
+
infer_yes_no,
|
| 13 |
+
infer_user_uncertainty,
|
| 14 |
+
infer_drill_type,
|
| 15 |
+
infer_eligibility,
|
| 16 |
+
extract_partner_info,
|
| 17 |
+
get_existing_partner,
|
| 18 |
+
register_partner,
|
| 19 |
+
validate_partner_url,
|
| 20 |
+
find_partner_by_url,
|
| 21 |
+
create_naming_assistant
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
__all__ = [
|
| 25 |
+
# Constants and models
|
| 26 |
+
"DEFAULT_DRILL_INFO",
|
| 27 |
+
"CATEGORY_SUBCATEGORY_MAP",
|
| 28 |
+
"get_llm",
|
| 29 |
+
"DateHandler",
|
| 30 |
+
"DatabaseHandler",
|
| 31 |
+
"infer_subcategory",
|
| 32 |
+
"infer_purpose",
|
| 33 |
+
"infer_yes_no",
|
| 34 |
+
"infer_user_uncertainty",
|
| 35 |
+
"infer_drill_type",
|
| 36 |
+
"infer_eligibility",
|
| 37 |
+
"recognize_name_intent",
|
| 38 |
+
"extract_partner_info",
|
| 39 |
+
"get_existing_partner",
|
| 40 |
+
"register_partner",
|
| 41 |
+
"validate_partner_url",
|
| 42 |
+
"find_partner_by_url",
|
| 43 |
+
"generate_drill_description",
|
| 44 |
+
"generate_drill_overview_description",
|
| 45 |
+
"generate_random_event_names",
|
| 46 |
+
"generate_faq_answers",
|
| 47 |
+
"create_naming_assistant",
|
| 48 |
+
|
| 49 |
+
]
|
src/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (1.21 kB). View file
|
|
|
src/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (709 Bytes). View file
|
|
|
src/__pycache__/constants.cpython-311.pyc
ADDED
|
Binary file (2.05 kB). View file
|
|
|
src/__pycache__/constants.cpython-312.pyc
ADDED
|
Binary file (1.55 kB). View file
|
|
|
src/__pycache__/main.cpython-311.pyc
ADDED
|
Binary file (16 kB). View file
|
|
|
src/__pycache__/models.cpython-311.pyc
ADDED
|
Binary file (504 Bytes). View file
|
|
|
src/__pycache__/models.cpython-312.pyc
ADDED
|
Binary file (456 Bytes). View file
|
|
|
src/__pycache__/questions.cpython-311.pyc
ADDED
|
Binary file (769 Bytes). View file
|
|
|
src/__pycache__/questions.cpython-312.pyc
ADDED
|
Binary file (790 Bytes). View file
|
|
|
src/__pycache__/testllmscrape.cpython-311-pytest-8.3.4.pyc
ADDED
|
Binary file (8.48 kB). View file
|
|
|
src/__pycache__/utils.cpython-311.pyc
ADDED
|
Binary file (56.4 kB). View file
|
|
|
src/__pycache__/utils.cpython-312.pyc
ADDED
|
Binary file (59 kB). View file
|
|
|
src/constants.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
DEFAULT_DRILL_INFO = {
|
| 2 |
+
"drillCategory": "",
|
| 3 |
+
"drillSubCategory": "",
|
| 4 |
+
"drillNature": "",
|
| 5 |
+
"drillDescription": "",
|
| 6 |
+
"drillName": "",
|
| 7 |
+
"drillPurpose": "",
|
| 8 |
+
"drillTimezone": "Asia/Kolkata",
|
| 9 |
+
"drillType": "",
|
| 10 |
+
"drillScheduleIfNotDateKnown": "",
|
| 11 |
+
"drillId": None,
|
| 12 |
+
"drillInvitationType": "PUBLIC",
|
| 13 |
+
"isDrillPaid": False,
|
| 14 |
+
"drillPhase": "{}",
|
| 15 |
+
"selectedSubCategoryLabel": "Hiring Hackathon",
|
| 16 |
+
"drillPartnerId": "b886470e-52ac-4d34-8621-ff3e4d8335fb",
|
| 17 |
+
"drillPartnerName": "WUElev8 Innovation services private ltd",
|
| 18 |
+
"drillRegistrationStartDt": "",
|
| 19 |
+
"drillRegistrationEndDt": ""
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
CATEGORY_SUBCATEGORY_MAP = {
|
| 23 |
+
"WORKSHOP": "SAARTHI",
|
| 24 |
+
"WEBINAR": "SAARTHI",
|
| 25 |
+
"MASTERCLASS": "SAARTHI",
|
| 26 |
+
"INNOVATIONHACKATHON": "OPENINNOVATION",
|
| 27 |
+
"SQUADPROGRAM": "OPENINNOVATION",
|
| 28 |
+
"CASESTUDY": "OPENINNOVATION",
|
| 29 |
+
"HIRINGHACKATHON": "OPENINNOVATION",
|
| 30 |
+
"INNOVATION": "OPENINNOVATION",
|
| 31 |
+
"TECHCONFERENCES": "HACKATHON",
|
| 32 |
+
"BOOTCAMP": "HACKATHON",
|
| 33 |
+
"STARTUP": "HACKATHON",
|
| 34 |
+
"CONCLAVE": "HACKATHON",
|
| 35 |
+
"CONTRACTUAL": "HACKATHON",
|
| 36 |
+
"IDEATHON": "HACKATHON",
|
| 37 |
+
"INTERNALHACKATHON": "HACKATHON",
|
| 38 |
+
"SMARTINDIAHACKATHON": "HACKATHON"
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
subcategory_display_map = {
|
| 42 |
+
"HIRINGHACKATHON": "Hiring Hackathon",
|
| 43 |
+
"INNOVATIONHACKATHON": "Innovation Hackathon",
|
| 44 |
+
"INTERNALHACKATHON": "Internal Hackathon",
|
| 45 |
+
"SMARTINDIAHACKATHON": "Smart India Hackathon",
|
| 46 |
+
"WORKSHOP": "Workshop",
|
| 47 |
+
"WEBINAR": "Webinar",
|
| 48 |
+
"BOOTCAMP": "Bootcamp",
|
| 49 |
+
"IDEATHON": "Ideathon",
|
| 50 |
+
"MASTERCLASS": "Masterclass",
|
| 51 |
+
"SQUADPROGRAM": "Squad Program",
|
| 52 |
+
"TECHCONFERENCES": "Tech Conferences",
|
| 53 |
+
"CASESTUDY": "Case Study",
|
| 54 |
+
"STARTUP": "Startup",
|
| 55 |
+
"CONCLAVE": "Conclave",
|
| 56 |
+
"CONTRACTUAL": "Contractual",
|
| 57 |
+
"INNOVATION": "Innovation"
|
| 58 |
+
}
|
src/main.py
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uuid
|
| 2 |
+
from typing import Any, Dict, List, Optional
|
| 3 |
+
|
| 4 |
+
import urllib3
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from fastapi import FastAPI, HTTPException
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
|
| 9 |
+
from src.constants import CATEGORY_SUBCATEGORY_MAP, DEFAULT_DRILL_INFO
|
| 10 |
+
from src.utils import (DatabaseHandler, DateHandler, create_naming_assistant,
|
| 11 |
+
extract_partner_info, find_partner_by_url,
|
| 12 |
+
generate_drill_description, get_llm, infer_drill_type,
|
| 13 |
+
infer_eligibility, infer_purpose, infer_subcategory,
|
| 14 |
+
infer_user_uncertainty, infer_yes_no,
|
| 15 |
+
prepare_api_payload, register_event, register_partner,
|
| 16 |
+
save_to_database, validate_partner_url)
|
| 17 |
+
|
| 18 |
+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
| 19 |
+
|
| 20 |
+
load_dotenv()
|
| 21 |
+
app = FastAPI()
|
| 22 |
+
|
| 23 |
+
date_handler = DateHandler(default_timezone="Asia/Kolkata")
|
| 24 |
+
db_handler = DatabaseHandler()
|
| 25 |
+
llm = get_llm()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class SessionInput(BaseModel):
|
| 29 |
+
session_id: Optional[str] = None
|
| 30 |
+
message: str
|
| 31 |
+
current_question: Optional[str] = None
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class SessionResponse(BaseModel):
|
| 35 |
+
session_id: str
|
| 36 |
+
message: str
|
| 37 |
+
event_link: Optional[str] = None
|
| 38 |
+
current_question: Optional[str] = None
|
| 39 |
+
registration_complete: bool = False
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
session_store: Dict[str, Dict[str, Any]] = {}
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@app.post("/chat", response_model=SessionResponse)
|
| 46 |
+
async def chat_endpoint(chat_input: SessionInput):
|
| 47 |
+
try:
|
| 48 |
+
session_id = chat_input.session_id or str(uuid.uuid4())
|
| 49 |
+
|
| 50 |
+
if session_id not in session_store:
|
| 51 |
+
session_store[session_id] = {
|
| 52 |
+
"context": {"hackathon_details": DEFAULT_DRILL_INFO.copy()},
|
| 53 |
+
"current_question": None,
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
session_data = session_store[session_id]
|
| 57 |
+
message = chat_input.message
|
| 58 |
+
context = session_data["context"]
|
| 59 |
+
current_question = (
|
| 60 |
+
chat_input.current_question or session_data["current_question"]
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
if not current_question:
|
| 64 |
+
session_data.update({"current_question": "drillSubCategory"})
|
| 65 |
+
return SessionResponse(
|
| 66 |
+
session_id=session_id,
|
| 67 |
+
message="What type of event you want to host : Workshop, Webinar, Masterclass, Innovation Hackathon, Squad Program, Case Study, Hiring Hackathon, Innovation, Tech Conferences, Bootcamp, Startup, Conclave, Contractual, Ideathon, Internal Hackathon, Smart India Hackathon?",
|
| 68 |
+
current_question="drillSubCategory",
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
if current_question == "drillSubCategory":
|
| 72 |
+
subcategory = infer_subcategory(message, CATEGORY_SUBCATEGORY_MAP, llm)
|
| 73 |
+
if not subcategory:
|
| 74 |
+
return SessionResponse(
|
| 75 |
+
session_id=session_id,
|
| 76 |
+
message="Could not determine a valid subcategory. Please try again.",
|
| 77 |
+
current_question="drillSubCategory",
|
| 78 |
+
)
|
| 79 |
+
context["hackathon_details"]["drillSubCategory"] = subcategory
|
| 80 |
+
context["hackathon_details"]["drillCategory"] = CATEGORY_SUBCATEGORY_MAP[
|
| 81 |
+
subcategory
|
| 82 |
+
]
|
| 83 |
+
|
| 84 |
+
session_data.update({"current_question": "drillName"})
|
| 85 |
+
return SessionResponse(
|
| 86 |
+
session_id=session_id,
|
| 87 |
+
message="What name do you want to give to your event?",
|
| 88 |
+
current_question="drillName",
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
if current_question == "drillName":
|
| 92 |
+
# Check if user is uncertain or needs help
|
| 93 |
+
if infer_user_uncertainty(message, llm) or session_store[session_id].get(
|
| 94 |
+
"in_naming_conversation"
|
| 95 |
+
):
|
| 96 |
+
# Initialize naming conversation if not exists
|
| 97 |
+
if "naming_assistant" not in session_store[session_id]:
|
| 98 |
+
naming_assistant = create_naming_assistant(llm)
|
| 99 |
+
session_store[session_id]["naming_assistant"] = naming_assistant
|
| 100 |
+
session_store[session_id]["in_naming_conversation"] = True
|
| 101 |
+
session_store[session_id]["naming_history"] = []
|
| 102 |
+
|
| 103 |
+
# Start the conversation
|
| 104 |
+
response = naming_assistant.invoke(
|
| 105 |
+
{
|
| 106 |
+
"input": "I need help naming my event. It's a "
|
| 107 |
+
+ context["hackathon_details"]["drillSubCategory"],
|
| 108 |
+
"history": [],
|
| 109 |
+
}
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
session_store[session_id]["naming_history"].append(
|
| 113 |
+
{"user": message, "assistant": response}
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
return SessionResponse(
|
| 117 |
+
session_id=session_id,
|
| 118 |
+
message=response,
|
| 119 |
+
current_question="drillName",
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# Continue naming conversation
|
| 123 |
+
naming_assistant = session_store[session_id]["naming_assistant"]
|
| 124 |
+
history = session_store[session_id]["naming_history"]
|
| 125 |
+
|
| 126 |
+
# Format history for the prompt
|
| 127 |
+
formatted_history = "\n".join(
|
| 128 |
+
[f"User: {h['user']}\nAssistant: {h['assistant']}" for h in history]
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
response = naming_assistant.invoke(
|
| 132 |
+
{"input": message, "history": formatted_history}
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# Update conversation history
|
| 136 |
+
history.append({"user": message, "assistant": response})
|
| 137 |
+
|
| 138 |
+
# Check if a name was selected
|
| 139 |
+
if "SELECTED_NAME:" in response:
|
| 140 |
+
selected_name = response.split("SELECTED_NAME:")[1].strip()
|
| 141 |
+
context["hackathon_details"]["drillName"] = selected_name
|
| 142 |
+
|
| 143 |
+
# Clean up naming conversation
|
| 144 |
+
session_store[session_id].pop("naming_assistant", None)
|
| 145 |
+
session_store[session_id].pop("in_naming_conversation", None)
|
| 146 |
+
session_store[session_id].pop("naming_history", None)
|
| 147 |
+
|
| 148 |
+
session_data.update({"current_question": "phaseStartDt"})
|
| 149 |
+
|
| 150 |
+
return SessionResponse(
|
| 151 |
+
session_id=session_id,
|
| 152 |
+
message=f"Perfect! Your event name is set to {selected_name}. When do you want to organize it?",
|
| 153 |
+
current_question="phaseStartDt",
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
return SessionResponse(
|
| 157 |
+
session_id=session_id,
|
| 158 |
+
message=response,
|
| 159 |
+
current_question="drillName",
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
# Direct name input
|
| 163 |
+
else:
|
| 164 |
+
context["hackathon_details"]["drillName"] = message
|
| 165 |
+
session_data.update({"current_question": "phaseStartDt"})
|
| 166 |
+
return SessionResponse(
|
| 167 |
+
session_id=session_id,
|
| 168 |
+
message=f"Great! Your event name is set to {message}. When do you want to organize it?",
|
| 169 |
+
current_question="phaseStartDt",
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
elif current_question == "phaseStartDt":
|
| 173 |
+
parsed_date = date_handler.parse_date_string(message)
|
| 174 |
+
|
| 175 |
+
if parsed_date is None:
|
| 176 |
+
return SessionResponse(
|
| 177 |
+
session_id=session_id,
|
| 178 |
+
message="I couldn't understand that date format. Please provide a date in DD-MM-YYYY format.",
|
| 179 |
+
current_question="phaseStartDt",
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
formatted_date = parsed_date.strftime("%d-%m-%Y")
|
| 183 |
+
|
| 184 |
+
is_valid, transformed_date, error_message = date_handler.validate_date(
|
| 185 |
+
formatted_date
|
| 186 |
+
)
|
| 187 |
+
if not is_valid:
|
| 188 |
+
return SessionResponse(
|
| 189 |
+
session_id=session_id,
|
| 190 |
+
message=error_message,
|
| 191 |
+
current_question="phaseStartDt",
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
phase_dates = date_handler.get_phase_dates(transformed_date, "Asia/Kolkata")
|
| 195 |
+
context["hackathon_details"].update(phase_dates)
|
| 196 |
+
|
| 197 |
+
return SessionResponse(
|
| 198 |
+
session_id=session_id,
|
| 199 |
+
message="Do you want submissions based on specific themes or solutions to problems? (Problems Based or Theme Based)",
|
| 200 |
+
current_question="drillType",
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
elif current_question == "drillType":
|
| 204 |
+
drill_type = infer_drill_type(message, llm)
|
| 205 |
+
context["hackathon_details"]["drillType"] = drill_type
|
| 206 |
+
return SessionResponse(
|
| 207 |
+
session_id=session_id,
|
| 208 |
+
message="Will this event be paid? (Yes/No)",
|
| 209 |
+
current_question="isDrillPaid",
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
elif current_question == "isDrillPaid":
|
| 213 |
+
is_paid = infer_yes_no(message, llm)
|
| 214 |
+
context["hackathon_details"]["isDrillPaid"] = is_paid == "Yes"
|
| 215 |
+
return SessionResponse(
|
| 216 |
+
session_id=session_id,
|
| 217 |
+
message="What is the purpose of this event? (Provide a one-liner answer)",
|
| 218 |
+
current_question="drillPurpose",
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
elif current_question == "drillPurpose":
|
| 222 |
+
purpose = infer_purpose(context, message, llm)
|
| 223 |
+
context["hackathon_details"]["drillPurpose"] = purpose
|
| 224 |
+
|
| 225 |
+
session_data.update({"current_question": "eligibility"})
|
| 226 |
+
return SessionResponse(
|
| 227 |
+
session_id=session_id,
|
| 228 |
+
message="Who can participate in this event? (Options: Graduate, College Students, Working Professionals)",
|
| 229 |
+
current_question="eligibility",
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
elif current_question == "eligibility":
|
| 233 |
+
eligibility = infer_eligibility(message, llm)
|
| 234 |
+
if not eligibility:
|
| 235 |
+
return SessionResponse(
|
| 236 |
+
session_id=session_id,
|
| 237 |
+
message="Could not determine eligibility. Please specify who can participate (Graduate, College Students, Working Professionals)",
|
| 238 |
+
current_question="eligibility",
|
| 239 |
+
)
|
| 240 |
+
context["hackathon_details"]["eligibility"] = eligibility
|
| 241 |
+
|
| 242 |
+
session_data.update({"current_question": "hasPartner"})
|
| 243 |
+
return SessionResponse(
|
| 244 |
+
session_id=session_id,
|
| 245 |
+
message="Do you want to add a partner organization for this event? (Yes/No)",
|
| 246 |
+
current_question="hasPartner",
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
elif current_question == "hasPartner":
|
| 250 |
+
has_partner = infer_yes_no(message, llm)
|
| 251 |
+
context["has_partner"] = has_partner == "Yes"
|
| 252 |
+
|
| 253 |
+
if context["has_partner"]:
|
| 254 |
+
session_data.update({"current_question": "partnerUrl"})
|
| 255 |
+
return SessionResponse(
|
| 256 |
+
session_id=session_id,
|
| 257 |
+
message="Please provide the website URL of the partner organization:",
|
| 258 |
+
current_question="partnerUrl",
|
| 259 |
+
)
|
| 260 |
+
else:
|
| 261 |
+
context["hackathon_details"].update(
|
| 262 |
+
{
|
| 263 |
+
"drillPartnerId": "b886470e-52ac-4d34-8621-ff3e4d8335fb",
|
| 264 |
+
"drillPartnerName": "WUElev8 Innovation services private ltd",
|
| 265 |
+
}
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
try:
|
| 269 |
+
drill_info = context["hackathon_details"]
|
| 270 |
+
drill_info["drillDescription"] = generate_drill_description(
|
| 271 |
+
drill_info, llm
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
api_payload = prepare_api_payload(drill_info)
|
| 275 |
+
event_link = register_event(api_payload, drill_info["eligibility"])
|
| 276 |
+
|
| 277 |
+
save_to_database(drill_info, db_handler)
|
| 278 |
+
session_store.pop(session_id, None)
|
| 279 |
+
|
| 280 |
+
return SessionResponse(
|
| 281 |
+
session_id=session_id,
|
| 282 |
+
message="Your event has been successfully registered!",
|
| 283 |
+
event_link=event_link,
|
| 284 |
+
registration_complete=True,
|
| 285 |
+
)
|
| 286 |
+
except Exception as e:
|
| 287 |
+
print(f"Registration error: {str(e)}")
|
| 288 |
+
return SessionResponse(
|
| 289 |
+
session_id=session_id,
|
| 290 |
+
message=f"An error occurred during registration: {str(e)}",
|
| 291 |
+
current_question=current_question,
|
| 292 |
+
)
|
| 293 |
+
|
| 294 |
+
elif current_question == "partnerUrl":
|
| 295 |
+
partner_url = message.strip()
|
| 296 |
+
|
| 297 |
+
try:
|
| 298 |
+
if not validate_partner_url(partner_url):
|
| 299 |
+
return SessionResponse(
|
| 300 |
+
session_id=session_id,
|
| 301 |
+
message="The provided URL seems to be invalid or inaccessible. Please provide a valid URL.",
|
| 302 |
+
current_question="partnerUrl",
|
| 303 |
+
)
|
| 304 |
+
|
| 305 |
+
existing_partner = find_partner_by_url(partner_url)
|
| 306 |
+
|
| 307 |
+
if existing_partner and "partnerId" in existing_partner:
|
| 308 |
+
partner_id = existing_partner["partnerId"]
|
| 309 |
+
partner_name = (
|
| 310 |
+
existing_partner.get("partnerDisplayName")
|
| 311 |
+
or existing_partner.get("partnerName")
|
| 312 |
+
or partner_url.split("/")[2]
|
| 313 |
+
.replace("www.", "")
|
| 314 |
+
.split(".")[0]
|
| 315 |
+
.title()
|
| 316 |
+
)
|
| 317 |
+
|
| 318 |
+
print(f"Using existing partner: {partner_name} (ID: {partner_id})")
|
| 319 |
+
else:
|
| 320 |
+
try:
|
| 321 |
+
partner_info = extract_partner_info(partner_url, llm)
|
| 322 |
+
|
| 323 |
+
partner_response = register_partner(partner_info)
|
| 324 |
+
|
| 325 |
+
if not partner_response or "partnerId" not in partner_response:
|
| 326 |
+
raise ValueError("Failed to register partner")
|
| 327 |
+
|
| 328 |
+
partner_id = partner_response["partnerId"]
|
| 329 |
+
partner_name = (
|
| 330 |
+
partner_response.get("partnerDisplayName")
|
| 331 |
+
or partner_response.get("partnerName")
|
| 332 |
+
or partner_url.split("/")[2]
|
| 333 |
+
.replace("www.", "")
|
| 334 |
+
.split(".")[0]
|
| 335 |
+
.title()
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
print(
|
| 339 |
+
f"Registered new partner: {partner_name} (ID: {partner_id})"
|
| 340 |
+
)
|
| 341 |
+
except Exception as e:
|
| 342 |
+
print(f"Partner extraction error: {str(e)}")
|
| 343 |
+
raise ValueError(
|
| 344 |
+
f"Failed to extract partner information: {str(e)}"
|
| 345 |
+
)
|
| 346 |
+
|
| 347 |
+
context["hackathon_details"].update(
|
| 348 |
+
{"drillPartnerId": partner_id, "drillPartnerName": partner_name}
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
drill_info = context["hackathon_details"]
|
| 352 |
+
drill_info["drillDescription"] = generate_drill_description(
|
| 353 |
+
drill_info, llm
|
| 354 |
+
)
|
| 355 |
+
|
| 356 |
+
api_payload = prepare_api_payload(drill_info)
|
| 357 |
+
event_link = register_event(api_payload, drill_info["eligibility"])
|
| 358 |
+
|
| 359 |
+
save_to_database(drill_info, db_handler)
|
| 360 |
+
session_store.pop(session_id, None)
|
| 361 |
+
|
| 362 |
+
return SessionResponse(
|
| 363 |
+
session_id=session_id,
|
| 364 |
+
message=f"Event successfully registered with partner {partner_name}!",
|
| 365 |
+
event_link=event_link,
|
| 366 |
+
registration_complete=True,
|
| 367 |
+
)
|
| 368 |
+
|
| 369 |
+
except Exception as e:
|
| 370 |
+
print(f"Partner registration error: {str(e)}")
|
| 371 |
+
return SessionResponse(
|
| 372 |
+
session_id=session_id,
|
| 373 |
+
message="There was an issue with the partner registration. Please try again with a different URL.",
|
| 374 |
+
current_question="partnerUrl",
|
| 375 |
+
)
|
| 376 |
+
|
| 377 |
+
except Exception as e:
|
| 378 |
+
print(f"Chat endpoint error: {str(e)}")
|
| 379 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
@app.get("/sessions/{session_id}/status")
|
| 383 |
+
async def get_session_status(session_id: str):
|
| 384 |
+
"""Get the current status of a session."""
|
| 385 |
+
if session_id not in session_store:
|
| 386 |
+
raise HTTPException(status_code=404, detail="Session not found")
|
| 387 |
+
return session_store[session_id]
|
| 388 |
+
|
| 389 |
+
|
| 390 |
+
@app.delete("/sessions/{session_id}")
|
| 391 |
+
async def clear_session(session_id: str):
|
| 392 |
+
"""Clear a session from memory."""
|
| 393 |
+
if session_id in session_store:
|
| 394 |
+
session_store.pop(session_id)
|
| 395 |
+
return {"message": "Session cleared"}
|
src/testllmscrape.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from unittest.mock import patch, MagicMock
|
| 3 |
+
import json
|
| 4 |
+
from src.utils.partner_utils import extract_partner_info # ✅ updated import
|
| 5 |
+
|
| 6 |
+
@pytest.fixture
|
| 7 |
+
def mock_html():
|
| 8 |
+
return "<html><head><title>Test Company</title></head><body>About Test Company</body></html>"
|
| 9 |
+
|
| 10 |
+
@pytest.fixture
|
| 11 |
+
def expected_output():
|
| 12 |
+
return {
|
| 13 |
+
"active": True,
|
| 14 |
+
"customUrl": "test-company",
|
| 15 |
+
"partnerDisplayName": "Test Company",
|
| 16 |
+
"partnerEstablishmentDate": "2010",
|
| 17 |
+
"partnerLogoPath": "https://test.com/logo.png",
|
| 18 |
+
"partnerName": "Test Company Pvt Ltd",
|
| 19 |
+
"partnerType": "SOFTWARE",
|
| 20 |
+
"partnerDescription": "Test Company provides software solutions.",
|
| 21 |
+
"partnerIndustry": "Technology",
|
| 22 |
+
"partnerSocialLinks": json.dumps({"websiteurl": "https://test.com"})
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
def test_extract_partner_info_success(mock_html, expected_output):
|
| 26 |
+
mock_llm = MagicMock()
|
| 27 |
+
mock_llm.invoke.return_value.content = f"""```json
|
| 28 |
+
{json.dumps(expected_output, indent=2)}
|
| 29 |
+
```"""
|
| 30 |
+
|
| 31 |
+
with patch("src.utils.partner_utils.scrape_website", return_value=mock_html):
|
| 32 |
+
result = extract_partner_info("test.com", llm=mock_llm)
|
| 33 |
+
|
| 34 |
+
assert result == expected_output
|
| 35 |
+
|
| 36 |
+
def test_extract_partner_info_llm_json_failure(mock_html):
|
| 37 |
+
mock_llm = MagicMock()
|
| 38 |
+
mock_llm.invoke.return_value.content = "<html without JSON>"
|
| 39 |
+
|
| 40 |
+
with patch("src.utils.partner_utils.scrape_website", return_value=mock_html):
|
| 41 |
+
result = extract_partner_info("test.com", llm=mock_llm)
|
| 42 |
+
|
| 43 |
+
assert result["partnerName"] == "Test"
|
| 44 |
+
assert result["customUrl"] == "test-com"
|
| 45 |
+
assert result["partnerSocialLinks"] == json.dumps({"websiteurl": "https://test.com"})
|
| 46 |
+
assert result["active"] is True
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_extract_partner_info_whereuelevate():
|
| 50 |
+
url = "https://whereuelevate.com/"
|
| 51 |
+
mock_html = "<html><body>Where U Elevate connects innovators through programs and opportunities.</body></html>"
|
| 52 |
+
|
| 53 |
+
expected_output = {
|
| 54 |
+
"active": True,
|
| 55 |
+
"customUrl": "whereuelevate",
|
| 56 |
+
"partnerDisplayName": "Where U Elevate",
|
| 57 |
+
"partnerEstablishmentDate": "",
|
| 58 |
+
"partnerLogoPath": "https://dfxy74dfhcx53.cloudfront.net/brand/primary_logo.webp",
|
| 59 |
+
"partnerName": "Where U Elevate",
|
| 60 |
+
"partnerType": "CAREER DEVELOPMENT",
|
| 61 |
+
"partnerDescription": "Where U Elevate connects innovators and organizations through Open Innovation Programs, Hackathons & Career Opportunities. Elevate your career and solve real-world problems.",
|
| 62 |
+
"partnerIndustry": "Open Innovation Programs, Hackathons & Career Opportunities",
|
| 63 |
+
"partnerSocialLinks": json.dumps({
|
| 64 |
+
"websiteurl": "https://whereuelevate.com/",
|
| 65 |
+
"twitter": "https://twitter.com/whereuelevate",
|
| 66 |
+
"linkedin": "https://www.linkedin.com/company/79681740",
|
| 67 |
+
"facebook": "https://www.facebook.com/whereuelevate",
|
| 68 |
+
"youtube": "https://www.youtube.com/@whereuelevate"
|
| 69 |
+
})
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
mock_llm = MagicMock()
|
| 73 |
+
mock_llm.invoke.return_value.content = f"""```json
|
| 74 |
+
{json.dumps(expected_output, indent=2)}
|
| 75 |
+
```"""
|
| 76 |
+
|
| 77 |
+
with patch("src.utils.partner_utils.scrape_website", return_value=mock_html):
|
| 78 |
+
result = extract_partner_info(url, llm=mock_llm)
|
| 79 |
+
|
| 80 |
+
assert result == expected_output
|
src/utils/__init__.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Core utilities
|
| 2 |
+
from .core import get_llm, llm
|
| 3 |
+
|
| 4 |
+
# Handlers and utility classes
|
| 5 |
+
from .date_handler import DateHandler
|
| 6 |
+
from .database import DatabaseHandler
|
| 7 |
+
|
| 8 |
+
# LLM inference utilities
|
| 9 |
+
from .llm_utils import (
|
| 10 |
+
infer_purpose,
|
| 11 |
+
infer_yes_no,
|
| 12 |
+
infer_user_uncertainty,
|
| 13 |
+
infer_drill_type,
|
| 14 |
+
infer_subcategory,
|
| 15 |
+
infer_eligibility
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Partner-related utilities
|
| 19 |
+
from .partner_utils import (
|
| 20 |
+
extract_partner_info,
|
| 21 |
+
get_existing_partner,
|
| 22 |
+
register_partner,
|
| 23 |
+
validate_partner_url,
|
| 24 |
+
find_partner_by_url
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Content generation utilities
|
| 28 |
+
from .content_generation import (
|
| 29 |
+
generate_drill_description,
|
| 30 |
+
generate_drill_overview_description,
|
| 31 |
+
generate_random_event_names,
|
| 32 |
+
generate_faq_answers,
|
| 33 |
+
create_naming_assistant
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# API and database utilities
|
| 37 |
+
from .api_utils import (
|
| 38 |
+
prepare_api_payload,
|
| 39 |
+
register_event,
|
| 40 |
+
process_eligibility_data,
|
| 41 |
+
create_event_overview,
|
| 42 |
+
create_event_faqs
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
from .database_utils import save_to_database
|
| 46 |
+
|
| 47 |
+
# Backwards compatibility function
|
| 48 |
+
def check_for_cancellation(user_response: str, llm=None) -> bool:
|
| 49 |
+
"""Use LLM to infer if the user wants to cancel the registration process."""
|
| 50 |
+
if llm is None:
|
| 51 |
+
from .core import llm
|
| 52 |
+
|
| 53 |
+
user_response = user_response.lower()
|
| 54 |
+
cancel_keywords = ['cancel', 'stop', 'quit', 'exit', 'abort', 'terminate', 'end',
|
| 55 |
+
'don\'t want to proceed', 'don\'t want to continue']
|
| 56 |
+
return any(keyword in user_response for keyword in cancel_keywords)
|
| 57 |
+
|
| 58 |
+
__all__ = [
|
| 59 |
+
# Core utilities
|
| 60 |
+
"get_llm",
|
| 61 |
+
"llm",
|
| 62 |
+
|
| 63 |
+
# Handlers
|
| 64 |
+
"DateHandler",
|
| 65 |
+
"DatabaseHandler",
|
| 66 |
+
"infer_subcategory",
|
| 67 |
+
"infer_purpose",
|
| 68 |
+
"infer_yes_no",
|
| 69 |
+
"infer_user_uncertainty",
|
| 70 |
+
"infer_drill_type",
|
| 71 |
+
"infer_eligibility",
|
| 72 |
+
"extract_partner_info",
|
| 73 |
+
"get_existing_partner",
|
| 74 |
+
"register_partner",
|
| 75 |
+
"validate_partner_url",
|
| 76 |
+
"find_partner_by_url",
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
"generate_drill_description",
|
| 80 |
+
"generate_drill_overview_description",
|
| 81 |
+
"generate_random_event_names",
|
| 82 |
+
"generate_faq_answers",
|
| 83 |
+
"create_naming_assistant",
|
| 84 |
+
|
| 85 |
+
# API utilities
|
| 86 |
+
"prepare_api_payload",
|
| 87 |
+
"register_event",
|
| 88 |
+
"process_eligibility_data",
|
| 89 |
+
"create_event_overview",
|
| 90 |
+
"create_event_faqs",
|
| 91 |
+
|
| 92 |
+
# Database utilities
|
| 93 |
+
"save_to_database",
|
| 94 |
+
|
| 95 |
+
# Backwards compatibility
|
| 96 |
+
"check_for_cancellation"
|
| 97 |
+
]
|
src/utils/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (2.49 kB). View file
|
|
|
src/utils/__pycache__/api_utils.cpython-311.pyc
ADDED
|
Binary file (9.07 kB). View file
|
|
|
src/utils/__pycache__/content_generation.cpython-311.pyc
ADDED
|
Binary file (10.4 kB). View file
|
|
|
src/utils/__pycache__/core.cpython-311.pyc
ADDED
|
Binary file (907 Bytes). View file
|
|
|
src/utils/__pycache__/database.cpython-311.pyc
ADDED
|
Binary file (5.17 kB). View file
|
|
|
src/utils/__pycache__/database_utils.cpython-311.pyc
ADDED
|
Binary file (2.14 kB). View file
|
|
|
src/utils/__pycache__/date_handler.cpython-311.pyc
ADDED
|
Binary file (8.9 kB). View file
|
|
|
src/utils/__pycache__/llm_utils.cpython-311.pyc
ADDED
|
Binary file (12.6 kB). View file
|
|
|
src/utils/__pycache__/partner_utils.cpython-311.pyc
ADDED
|
Binary file (15.5 kB). View file
|
|
|
src/utils/__pycache__/testllmscrape.cpython-311-pytest-8.3.4.pyc
ADDED
|
Binary file (5.44 kB). View file
|
|
|
src/utils/api_utils.py
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from typing import Dict, List, Any
|
| 6 |
+
from fastapi import HTTPException
|
| 7 |
+
from src.constants import subcategory_display_map
|
| 8 |
+
|
| 9 |
+
def prepare_api_payload(drill_info: dict) -> dict:
|
| 10 |
+
"""
|
| 11 |
+
Prepare the API payload for event registration.
|
| 12 |
+
"""
|
| 13 |
+
phase_start_date = datetime.strptime(drill_info["phaseStartDt"], "%d-%m-%Y")
|
| 14 |
+
phase_end_date = datetime.strptime(drill_info["phaseEndDt"], "%d-%m-%Y")
|
| 15 |
+
registration_start_date = datetime.strptime(
|
| 16 |
+
drill_info["drillRegistrationStartDt"], "%d-%m-%Y"
|
| 17 |
+
)
|
| 18 |
+
registration_end_date = datetime.strptime(
|
| 19 |
+
drill_info["drillRegistrationEndDt"], "%d-%m-%Y"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
drill_phase = {
|
| 23 |
+
"type": "Single",
|
| 24 |
+
"hasIdeaPhase": "",
|
| 25 |
+
"isSelfPaced": False,
|
| 26 |
+
"schedule": [
|
| 27 |
+
{
|
| 28 |
+
"phaseType": drill_info["drillCategory"],
|
| 29 |
+
"phaseDesc": "Hackathon Phase",
|
| 30 |
+
"dateConfirmed": True,
|
| 31 |
+
"phaseStartDt": phase_start_date.strftime("%Y-%m-%dT%H:%M:%S.000Z"),
|
| 32 |
+
"phaseEndDt": phase_end_date.strftime("%Y-%m-%dT%H:%M:%S.000Z"),
|
| 33 |
+
"phaseSubmissionEndDt": None,
|
| 34 |
+
"isSubmissionAllowed": False,
|
| 35 |
+
"phaseName": "Phase 1",
|
| 36 |
+
"phaseTimezone": "Asia/Kolkata",
|
| 37 |
+
"phaseMode": "Online",
|
| 38 |
+
"phasePosition": 0,
|
| 39 |
+
}
|
| 40 |
+
],
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
partner_id = drill_info.get(
|
| 44 |
+
"drillPartnerId", "b886470e-52ac-4d34-8621-ff3e4d8335fb"
|
| 45 |
+
)
|
| 46 |
+
partner_name = drill_info.get(
|
| 47 |
+
"drillPartnerName", "WUElev8 Innovation services private ltd"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
subcategory = drill_info["drillSubCategory"].upper()
|
| 51 |
+
|
| 52 |
+
if subcategory in subcategory_display_map:
|
| 53 |
+
formatted_subcategory = subcategory_display_map[subcategory]
|
| 54 |
+
else:
|
| 55 |
+
|
| 56 |
+
formatted_subcategory = subcategory.capitalize()
|
| 57 |
+
|
| 58 |
+
return {
|
| 59 |
+
"drillCategory": drill_info["drillCategory"],
|
| 60 |
+
"drillSubCategory": drill_info["drillSubCategory"],
|
| 61 |
+
"drillNature": "",
|
| 62 |
+
"drillDescription": drill_info["drillDescription"],
|
| 63 |
+
"drillName": drill_info["drillName"],
|
| 64 |
+
"drillPurpose": drill_info["drillPurpose"],
|
| 65 |
+
"drillTimezone": "Asia/Kolkata",
|
| 66 |
+
"drillType": drill_info["drillType"],
|
| 67 |
+
"drillScheduleIfNotDateKnown": "",
|
| 68 |
+
"drillId": None,
|
| 69 |
+
"drillInvitationType": "PUBLIC",
|
| 70 |
+
"isDrillPaid": drill_info["isDrillPaid"],
|
| 71 |
+
"drillPhase": json.dumps(drill_phase),
|
| 72 |
+
"selectedSubCategoryLabel": formatted_subcategory,
|
| 73 |
+
"drillPartnerId": partner_id,
|
| 74 |
+
"drillPartnerName": partner_name,
|
| 75 |
+
"drillRegistrationStartDt": registration_start_date.strftime(
|
| 76 |
+
"%Y-%m-%dT%H:%M:%S.000Z"
|
| 77 |
+
),
|
| 78 |
+
"drillRegistrationEndDt": registration_end_date.strftime(
|
| 79 |
+
"%Y-%m-%dT%H:%M:%S.000Z"
|
| 80 |
+
),
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
def register_event(payload: dict, eligibility_data) -> str:
|
| 84 |
+
"""
|
| 85 |
+
Register an event with the API and set up FAQs.
|
| 86 |
+
Returns the event link URL.
|
| 87 |
+
"""
|
| 88 |
+
session_id = str(payload.get("session_id", ""))
|
| 89 |
+
|
| 90 |
+
api_url = os.getenv("DRILL_URL")
|
| 91 |
+
headers = {"Content-Type": "application/json", "Accept": "application/json"}
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
|
| 95 |
+
response = requests.post(api_url, json=payload, headers=headers)
|
| 96 |
+
|
| 97 |
+
response.raise_for_status()
|
| 98 |
+
api_response = response.json()
|
| 99 |
+
|
| 100 |
+
cust_url = api_response.get("drillCustUrl")
|
| 101 |
+
drill_id = api_response.get("drillId")
|
| 102 |
+
|
| 103 |
+
if not cust_url or not drill_id:
|
| 104 |
+
raise ValueError(
|
| 105 |
+
f"Invalid API response: missing drillCustUrl or drillId. Response: {api_response}"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
eligibility_json = process_eligibility_data(eligibility_data)
|
| 109 |
+
|
| 110 |
+
create_event_overview(drill_id, eligibility_json, payload["drillDescription"], headers)
|
| 111 |
+
|
| 112 |
+
from .core import get_llm
|
| 113 |
+
from .content_generation import generate_faq_answers
|
| 114 |
+
|
| 115 |
+
drill_info = payload.copy()
|
| 116 |
+
drill_info["drillId"] = drill_id
|
| 117 |
+
faq_answers = generate_faq_answers(drill_info, get_llm())
|
| 118 |
+
create_event_faqs(faq_answers, headers)
|
| 119 |
+
|
| 120 |
+
event_link = f"https://dev.whereuelevate.com/drills/{cust_url}"
|
| 121 |
+
|
| 122 |
+
return event_link
|
| 123 |
+
|
| 124 |
+
except requests.exceptions.RequestException as e:
|
| 125 |
+
print(f"API Error: {str(e)}")
|
| 126 |
+
if hasattr(e, "response") and e.response is not None:
|
| 127 |
+
print(f"API Error Response: {e.response.text}")
|
| 128 |
+
print(f"Status code: {e.response.status_code}")
|
| 129 |
+
print(f"Headers: {e.response.headers}")
|
| 130 |
+
raise HTTPException(
|
| 131 |
+
status_code=500,
|
| 132 |
+
detail=f"API Error: {str(e)}\nResponse: {e.response.text if hasattr(e, 'response') and e.response is not None else 'No response'}",
|
| 133 |
+
)
|
| 134 |
+
except Exception as e:
|
| 135 |
+
print(f"Unexpected error: {str(e)}")
|
| 136 |
+
raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
|
| 137 |
+
|
| 138 |
+
def process_eligibility_data(eligibility_data) -> str:
|
| 139 |
+
"""Process eligibility data into a JSON string."""
|
| 140 |
+
if isinstance(eligibility_data, list):
|
| 141 |
+
return json.dumps(eligibility_data)
|
| 142 |
+
elif isinstance(eligibility_data, str):
|
| 143 |
+
try:
|
| 144 |
+
json.loads(eligibility_data)
|
| 145 |
+
return eligibility_data
|
| 146 |
+
except json.JSONDecodeError:
|
| 147 |
+
return json.dumps(
|
| 148 |
+
[item.strip() for item in eligibility_data.split(",")]
|
| 149 |
+
)
|
| 150 |
+
else:
|
| 151 |
+
return json.dumps(
|
| 152 |
+
["Graduate", "Working Professionals", "College Students"]
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
def create_event_overview(drill_id: str, eligibility_json: str, description: str, headers: Dict[str, str]) -> None:
|
| 156 |
+
"""Create the overview section for an event."""
|
| 157 |
+
overview_api_url = os.getenv("OVERVIEW_URL")
|
| 158 |
+
|
| 159 |
+
overview_payload = {
|
| 160 |
+
"drillId": drill_id,
|
| 161 |
+
"eligibility": eligibility_json,
|
| 162 |
+
"guidelines": None,
|
| 163 |
+
"domain": None,
|
| 164 |
+
"industry": None,
|
| 165 |
+
"totalPrizeValue": None,
|
| 166 |
+
"createdTs": datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
|
| 167 |
+
+ "+00:00",
|
| 168 |
+
"updatedTs": datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
|
| 169 |
+
+ "+00:00",
|
| 170 |
+
"createdBy": None,
|
| 171 |
+
"updatedBy": None,
|
| 172 |
+
"overviewSectionComplete": None,
|
| 173 |
+
"drillDescription": description,
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
overview_response = requests.post(
|
| 178 |
+
overview_api_url, json=overview_payload, headers=headers
|
| 179 |
+
)
|
| 180 |
+
overview_response.raise_for_status()
|
| 181 |
+
|
| 182 |
+
def create_event_faqs(faq_answers: List[Dict[str, Any]], headers: Dict[str, str]) -> None:
|
| 183 |
+
"""Create FAQs for an event."""
|
| 184 |
+
faq_api_url = os.getenv("FAQ_URL")
|
| 185 |
+
|
| 186 |
+
for faq in faq_answers:
|
| 187 |
+
faq_response = requests.post(faq_api_url, json=faq, headers=headers)
|
| 188 |
+
faq_response.raise_for_status()
|
| 189 |
+
|
src/utils/content_generation.py
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from typing import List
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 5 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 6 |
+
from langchain_core.runnables import RunnableSequence
|
| 7 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
| 8 |
+
|
| 9 |
+
from .core import get_llm
|
| 10 |
+
|
| 11 |
+
def generate_drill_description(drill_info: dict, llm=None) -> str:
|
| 12 |
+
"""Generate a short description for the event based on the drillPurpose."""
|
| 13 |
+
if llm is None:
|
| 14 |
+
llm = get_llm()
|
| 15 |
+
|
| 16 |
+
drill_purpose = drill_info.get("drillPurpose", "Innovation")
|
| 17 |
+
|
| 18 |
+
description_prompt = f"""
|
| 19 |
+
Generate a short and engaging description for the following event:
|
| 20 |
+
- Name: {drill_info["drillName"]}
|
| 21 |
+
- Type: {drill_info["drillType"]}
|
| 22 |
+
- Purpose: {drill_purpose}
|
| 23 |
+
|
| 24 |
+
The description should clearly reflect the purpose of the event and provide an overview that aligns with the event's goals.
|
| 25 |
+
"""
|
| 26 |
+
prompt_template = ChatPromptTemplate.from_messages([
|
| 27 |
+
("system", description_prompt),
|
| 28 |
+
("human", "Please generate the description based on the above details.")
|
| 29 |
+
])
|
| 30 |
+
|
| 31 |
+
response = (prompt_template | llm).invoke({})
|
| 32 |
+
return response.content.strip()
|
| 33 |
+
|
| 34 |
+
def generate_drill_overview_description(drill_info: dict, llm=None) -> str:
|
| 35 |
+
"""Generate an overview description for the event."""
|
| 36 |
+
if llm is None:
|
| 37 |
+
llm = get_llm()
|
| 38 |
+
|
| 39 |
+
description_prompt = f"""
|
| 40 |
+
Generate a single, concise overview paragraph (max 2-3 sentences) for this event:
|
| 41 |
+
Name: {drill_info["drillName"]}
|
| 42 |
+
Type: {drill_info["drillType"]}
|
| 43 |
+
Purpose: {drill_info["drillPurpose"]}
|
| 44 |
+
Start Date: {drill_info["phaseStartDt"]}
|
| 45 |
+
End Date: {drill_info["phaseEndDt"]}
|
| 46 |
+
Registration Start Date: {drill_info["drillRegistrationStartDt"]}
|
| 47 |
+
Registration End Date: {drill_info["drillRegistrationEndDt"]}
|
| 48 |
+
Is Paid: {"Yes" if drill_info["isDrillPaid"] else "No"}
|
| 49 |
+
|
| 50 |
+
Rules:
|
| 51 |
+
- Create ONLY ONE paragraph with 2-3 sentences maximum
|
| 52 |
+
- Focus on the event's purpose and key details
|
| 53 |
+
- Keep it professional and engaging
|
| 54 |
+
- Do not list dates or repeat information verbatim
|
| 55 |
+
- Do not use bullet points or multiple paragraphs
|
| 56 |
+
- The response must be a single concise paragraph only
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
chain = (
|
| 60 |
+
ChatPromptTemplate.from_messages([
|
| 61 |
+
("system", description_prompt),
|
| 62 |
+
("human", "Generate a single overview paragraph.")
|
| 63 |
+
])
|
| 64 |
+
| llm
|
| 65 |
+
| StrOutputParser()
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
return chain.invoke({})
|
| 69 |
+
|
| 70 |
+
def generate_random_event_names(llm=None) -> List[str]:
|
| 71 |
+
"""Generate random creative event names when the user is unsure."""
|
| 72 |
+
if llm is None:
|
| 73 |
+
llm = get_llm()
|
| 74 |
+
|
| 75 |
+
prompt = """
|
| 76 |
+
Generate 5 random, creative, and professional event names.
|
| 77 |
+
Only return the names, each on a new line.
|
| 78 |
+
"""
|
| 79 |
+
|
| 80 |
+
response = llm.invoke(prompt)
|
| 81 |
+
suggestions = response.content.strip().split("\n")
|
| 82 |
+
return [name.strip() for name in suggestions if name.strip()]
|
| 83 |
+
|
| 84 |
+
def generate_faq_answers(drill_info: dict, llm=None) -> List[dict]:
|
| 85 |
+
"""Generate FAQ answers based on the drill details."""
|
| 86 |
+
drill_title = drill_info.get("drillName", "the hackathon")
|
| 87 |
+
is_paid = drill_info.get("isDrillPaid", False)
|
| 88 |
+
|
| 89 |
+
def format_date(date_str):
|
| 90 |
+
if not date_str:
|
| 91 |
+
return ""
|
| 92 |
+
try:
|
| 93 |
+
if "T" in date_str:
|
| 94 |
+
dt = datetime.strptime(date_str.split('T')[0], "%Y-%m-%d")
|
| 95 |
+
return dt.strftime("%d %B %Y")
|
| 96 |
+
else:
|
| 97 |
+
dt = datetime.strptime(date_str, "%d-%m-%Y")
|
| 98 |
+
return dt.strftime("%d %B %Y")
|
| 99 |
+
except Exception:
|
| 100 |
+
return date_str
|
| 101 |
+
|
| 102 |
+
drill_phase = json.loads(drill_info.get("drillPhase", "{}")) if isinstance(drill_info.get("drillPhase"), str) else drill_info.get("drillPhase", {})
|
| 103 |
+
phase_schedule = drill_phase.get("schedule", [{}])[0] if drill_phase.get("schedule") else {}
|
| 104 |
+
|
| 105 |
+
phase_start_dt = format_date(phase_schedule.get("phaseStartDt") or drill_info.get("phaseStartDt", ""))
|
| 106 |
+
phase_end_dt = format_date(phase_schedule.get("phaseEndDt") or drill_info.get("phaseEndDt", ""))
|
| 107 |
+
registration_start_dt = format_date(drill_info.get("drillRegistrationStartDt", ""))
|
| 108 |
+
registration_end_dt = format_date(drill_info.get("drillRegistrationEndDt", ""))
|
| 109 |
+
|
| 110 |
+
payment_type = "paid" if is_paid else "free"
|
| 111 |
+
payment_question = "Is this a free hackathon?"
|
| 112 |
+
payment_answer = (
|
| 113 |
+
"<p>No, this is a paid hackathon.</p>" if is_paid else
|
| 114 |
+
"<p>Yes, this is a free hackathon. There is no registration fee required to participate.</p>"
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
if phase_start_dt and phase_end_dt:
|
| 118 |
+
start_date_answer = (
|
| 119 |
+
f"The hackathon will start on {phase_start_dt} and end on {phase_end_dt}. "
|
| 120 |
+
f"Registration is open from {registration_start_dt} to {registration_end_dt}."
|
| 121 |
+
)
|
| 122 |
+
else:
|
| 123 |
+
start_date_answer = "Start date to be announced"
|
| 124 |
+
|
| 125 |
+
print(f"Debug - Dates: Start={phase_start_dt}, End={phase_end_dt}, Reg Start={registration_start_dt}, Reg End={registration_end_dt}")
|
| 126 |
+
print(f"Debug - Final answer: {start_date_answer}")
|
| 127 |
+
|
| 128 |
+
faq_list = [
|
| 129 |
+
{
|
| 130 |
+
"answer": "<p>To Participate – Click on the \"Login & Participate\" button and sign up using your email or login with your existing account. If you are already logged in then click on the \"Participate\" button to continue to join the hackathon.</p>",
|
| 131 |
+
"drillId": drill_info.get("drillId", ""),
|
| 132 |
+
"positionOrder": 0,
|
| 133 |
+
"question": f"How do I participate in {drill_title}?",
|
| 134 |
+
"questionId": ""
|
| 135 |
+
},
|
| 136 |
+
{
|
| 137 |
+
"answer": payment_answer,
|
| 138 |
+
"drillId": drill_info.get("drillId", ""),
|
| 139 |
+
"positionOrder": 1,
|
| 140 |
+
"question": payment_question,
|
| 141 |
+
"questionId": ""
|
| 142 |
+
},
|
| 143 |
+
{
|
| 144 |
+
"answer": "<p>Yes</p>",
|
| 145 |
+
"drillId": drill_info.get("drillId", ""),
|
| 146 |
+
"positionOrder": 2,
|
| 147 |
+
"question": "Will I receive a certificate for participation?",
|
| 148 |
+
"questionId": ""
|
| 149 |
+
},
|
| 150 |
+
{
|
| 151 |
+
"answer": f"<p>{start_date_answer}</p>",
|
| 152 |
+
"drillId": drill_info.get("drillId", ""),
|
| 153 |
+
"positionOrder": 3,
|
| 154 |
+
"question": "When will the hackathon start?",
|
| 155 |
+
"questionId": ""
|
| 156 |
+
}
|
| 157 |
+
]
|
| 158 |
+
|
| 159 |
+
return faq_list
|
| 160 |
+
|
| 161 |
+
def create_naming_assistant(llm=None) -> RunnableSequence:
|
| 162 |
+
"""Creates a naming assistant chain using ChatPromptTemplate"""
|
| 163 |
+
if llm is None:
|
| 164 |
+
llm = get_llm()
|
| 165 |
+
|
| 166 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 167 |
+
("system", """You are a conversational naming assistant helping users name their technology related event.
|
| 168 |
+
|
| 169 |
+
Key Behaviors:
|
| 170 |
+
1. Guide the conversation naturally to help users select a name
|
| 171 |
+
2. Keep your responses decent and engaging.
|
| 172 |
+
3. When user is uncertain, first ask about their event's theme/focus
|
| 173 |
+
4. Based on their response, provide 5 creative, relevant name suggestions
|
| 174 |
+
5. Handle user responses intelligently:
|
| 175 |
+
- If they select a suggestion (by number or name),or write a name from the suggestions shown, confirm with "SELECTED_NAME: [name]"
|
| 176 |
+
- If they want more options, provide new suggestions
|
| 177 |
+
- If they propose their own name, evaluate it and either accept ("SELECTED_NAME: [name]") or suggest alternatives
|
| 178 |
+
- If they're unsure, encourage them with refined suggestions
|
| 179 |
+
|
| 180 |
+
Format your suggestions as:
|
| 181 |
+
"Based on [their theme/focus], here are some suggestions:
|
| 182 |
+
1. [Name 1]
|
| 183 |
+
2. [Name 2]
|
| 184 |
+
3. [Name 3]
|
| 185 |
+
4. [Name 4]
|
| 186 |
+
5. [Name 5]
|
| 187 |
+
|
| 188 |
+
You can select one of these numbers, suggest your own name, or ask for more options."
|
| 189 |
+
|
| 190 |
+
IMPORTANT: Once a name is selected, immediately respond with "SELECTED_NAME: [name]" to signal completion.
|
| 191 |
+
|
| 192 |
+
Conversation History: {history}
|
| 193 |
+
Current User Input: {input}
|
| 194 |
+
"""),
|
| 195 |
+
("human", "{input}")
|
| 196 |
+
])
|
| 197 |
+
|
| 198 |
+
return prompt | llm | StrOutputParser()
|
src/utils/core.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
+
def get_llm():
|
| 8 |
+
"""Create and return a ChatGoogleGenerativeAI instance with proper authentication."""
|
| 9 |
+
google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 10 |
+
if not google_api_key:
|
| 11 |
+
raise ValueError("GOOGLE_API_KEY environment variable is not set")
|
| 12 |
+
return ChatGoogleGenerativeAI(
|
| 13 |
+
model="gemini-2.0-flash",
|
| 14 |
+
temperature=0,
|
| 15 |
+
google_api_key=google_api_key
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Global LLM instance for reuse
|
| 19 |
+
llm = get_llm()
|
src/utils/database.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pymysql
|
| 3 |
+
from typing import List
|
| 4 |
+
|
| 5 |
+
class DatabaseHandler:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
|
| 8 |
+
self.host = os.getenv("DB_HOST")
|
| 9 |
+
self.user = os.getenv("DB_USER")
|
| 10 |
+
self.password = os.getenv("DB_PASSWORD")
|
| 11 |
+
self.database = os.getenv("DB_NAME")
|
| 12 |
+
self.connection = None
|
| 13 |
+
|
| 14 |
+
def connect(self):
|
| 15 |
+
try:
|
| 16 |
+
self.connection = pymysql.connect(
|
| 17 |
+
host=self.host,
|
| 18 |
+
user=self.user,
|
| 19 |
+
password=self.password,
|
| 20 |
+
database=self.database
|
| 21 |
+
)
|
| 22 |
+
except pymysql.MySQLError as e:
|
| 23 |
+
print(f"Error connecting to MySQL: {e}")
|
| 24 |
+
|
| 25 |
+
def close(self):
|
| 26 |
+
|
| 27 |
+
if self.connection:
|
| 28 |
+
self.connection.close()
|
| 29 |
+
|
| 30 |
+
def create_table_if_not_exists(self, table_name: str, columns: List[str]):
|
| 31 |
+
if not self.connection:
|
| 32 |
+
self.connect()
|
| 33 |
+
|
| 34 |
+
column_definitions = []
|
| 35 |
+
for col in columns:
|
| 36 |
+
if col == "isDrillPaid":
|
| 37 |
+
column_definitions.append(f"`{col}` TINYINT(1)")
|
| 38 |
+
elif col == "phaseStartDt":
|
| 39 |
+
column_definitions.append(f"`{col}` VARCHAR(10)")
|
| 40 |
+
else:
|
| 41 |
+
column_definitions.append(f"`{col}` VARCHAR(255)")
|
| 42 |
+
|
| 43 |
+
columns_sql = ", ".join(column_definitions)
|
| 44 |
+
|
| 45 |
+
query = f"""
|
| 46 |
+
CREATE TABLE IF NOT EXISTS `{table_name}` (
|
| 47 |
+
{columns_sql},
|
| 48 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 49 |
+
);
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
with self.connection.cursor() as cursor:
|
| 54 |
+
cursor.execute(query)
|
| 55 |
+
self.connection.commit()
|
| 56 |
+
except pymysql.MySQLError as e:
|
| 57 |
+
print(f"Error creating table: {e}")
|
| 58 |
+
raise
|
| 59 |
+
|
| 60 |
+
def insert_record(self, table_name: str, record: dict):
|
| 61 |
+
|
| 62 |
+
if not self.connection:
|
| 63 |
+
self.connect()
|
| 64 |
+
|
| 65 |
+
columns = ", ".join([f"`{key}`" for key in record.keys()])
|
| 66 |
+
placeholders = ", ".join(["%s"] * len(record))
|
| 67 |
+
query = f"INSERT INTO `{table_name}` ({columns}) VALUES ({placeholders});"
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
with self.connection.cursor() as cursor:
|
| 71 |
+
cursor.execute(query, list(record.values()))
|
| 72 |
+
self.connection.commit()
|
| 73 |
+
except pymysql.MySQLError as e:
|
| 74 |
+
print(f"Error inserting record: {e}")
|
| 75 |
+
raise
|
src/utils/database_utils.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any
|
| 2 |
+
from fastapi import HTTPException
|
| 3 |
+
|
| 4 |
+
from .database import DatabaseHandler
|
| 5 |
+
|
| 6 |
+
def save_to_database(drill_info: Dict[str, Any], db_handler: DatabaseHandler = None) -> None:
|
| 7 |
+
"""
|
| 8 |
+
Save drill information to the database.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
drill_info: Dictionary containing event details
|
| 12 |
+
db_handler: Database handler instance (optional, will create one if not provided)
|
| 13 |
+
|
| 14 |
+
Raises:
|
| 15 |
+
HTTPException: If there's an error saving to the database
|
| 16 |
+
"""
|
| 17 |
+
try:
|
| 18 |
+
if db_handler is None:
|
| 19 |
+
db_handler = DatabaseHandler()
|
| 20 |
+
|
| 21 |
+
required_columns = [
|
| 22 |
+
"drillSubCategory",
|
| 23 |
+
"drillName",
|
| 24 |
+
"phaseStartDt",
|
| 25 |
+
"drillType",
|
| 26 |
+
"isDrillPaid",
|
| 27 |
+
"drillPurpose",
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
db_handler.create_table_if_not_exists("event_records", required_columns)
|
| 31 |
+
|
| 32 |
+
filtered_record = {}
|
| 33 |
+
for column in required_columns:
|
| 34 |
+
value = drill_info.get(column)
|
| 35 |
+
if column == "isDrillPaid":
|
| 36 |
+
filtered_record[column] = 1 if value else 0
|
| 37 |
+
elif column == "phaseStartDt":
|
| 38 |
+
from datetime import datetime
|
| 39 |
+
if isinstance(value, datetime):
|
| 40 |
+
filtered_record[column] = value.strftime("%d-%m-%Y")
|
| 41 |
+
else:
|
| 42 |
+
filtered_record[column] = value
|
| 43 |
+
else:
|
| 44 |
+
filtered_record[column] = value
|
| 45 |
+
|
| 46 |
+
db_handler.insert_record("hackathon_records", filtered_record)
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
error_msg = f"Database Error: {str(e)}"
|
| 50 |
+
print(error_msg)
|
| 51 |
+
raise HTTPException(status_code=500, detail=error_msg)
|
src/utils/date_handler.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from datetime import datetime, timedelta, timezone
|
| 3 |
+
import pytz
|
| 4 |
+
from dateutil import parser
|
| 5 |
+
from typing import Optional, Tuple, Dict
|
| 6 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 7 |
+
|
| 8 |
+
from .core import get_llm
|
| 9 |
+
|
| 10 |
+
class DateHandler:
|
| 11 |
+
|
| 12 |
+
def __init__(self, default_timezone: str = "UTC"):
|
| 13 |
+
self.default_timezone = pytz.timezone(default_timezone)
|
| 14 |
+
self.llm = get_llm()
|
| 15 |
+
|
| 16 |
+
def parse_date_string(self, date_string: str) -> Optional[datetime]:
|
| 17 |
+
"""Parse date string using LLM and fallback to traditional parsing."""
|
| 18 |
+
try:
|
| 19 |
+
|
| 20 |
+
date_prompt = ChatPromptTemplate.from_messages([
|
| 21 |
+
("system", f"""
|
| 22 |
+
Convert the following natural language date input into a specific date in DD-MM-YYYY format.
|
| 23 |
+
Input: {date_string}
|
| 24 |
+
|
| 25 |
+
Rules:
|
| 26 |
+
- If the year is not specified, use the current year
|
| 27 |
+
- If month is not specified, use the next occurrence of that date
|
| 28 |
+
- For relative dates (tomorrow, next week, etc.), calculate from current date
|
| 29 |
+
- Handle phrases like "5th may", "7 march", "next monday", "tomorrow", "next week"
|
| 30 |
+
- If input is ambiguous, make a reasonable assumption and pick the next occurrence
|
| 31 |
+
- Return ONLY the date in DD-MM-YYYY format or "INVALID" if cannot be interpreted
|
| 32 |
+
|
| 33 |
+
Current date: {datetime.now().strftime('%d-%m-%Y')}
|
| 34 |
+
"""),
|
| 35 |
+
("human", "Please convert the date.")
|
| 36 |
+
])
|
| 37 |
+
|
| 38 |
+
response = (date_prompt | self.llm).invoke({})
|
| 39 |
+
llm_date = response.content.strip()
|
| 40 |
+
|
| 41 |
+
if llm_date != "INVALID":
|
| 42 |
+
try:
|
| 43 |
+
return datetime.strptime(llm_date, "%d-%m-%Y")
|
| 44 |
+
except ValueError:
|
| 45 |
+
pass
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
parsed_date = parser.parse(date_string, dayfirst=True, fuzzy=True)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
if parsed_date.year == 1900:
|
| 52 |
+
current_year = datetime.now().year
|
| 53 |
+
parsed_date = parsed_date.replace(year=current_year)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
current_date = datetime.now()
|
| 57 |
+
if parsed_date.date() < current_date.date():
|
| 58 |
+
if parsed_date.month < current_date.month:
|
| 59 |
+
parsed_date = parsed_date.replace(year=current_year + 1)
|
| 60 |
+
elif parsed_date.month == current_date.month and parsed_date.day < current_date.day:
|
| 61 |
+
parsed_date = parsed_date.replace(year=current_year + 1)
|
| 62 |
+
|
| 63 |
+
return parsed_date
|
| 64 |
+
|
| 65 |
+
except (ValueError, TypeError):
|
| 66 |
+
|
| 67 |
+
formats = [
|
| 68 |
+
"%d-%m-%Y", "%d/%m/%Y", "%m-%d-%Y", "%m/%d/%Y",
|
| 69 |
+
"%Y-%m-%d", "%Y/%m/%d", "%d.%m.%Y", "%m.%d.%Y",
|
| 70 |
+
"%d %b %Y", "%d %B %Y", "%b %d %Y", "%B %d %Y"
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
+
for fmt in formats:
|
| 74 |
+
try:
|
| 75 |
+
return datetime.strptime(date_string, fmt)
|
| 76 |
+
except ValueError:
|
| 77 |
+
continue
|
| 78 |
+
|
| 79 |
+
final_prompt = ChatPromptTemplate.from_messages([
|
| 80 |
+
("system", f"""
|
| 81 |
+
The user has provided this date input: '{date_string}'
|
| 82 |
+
Please interpret this as a future date, considering:
|
| 83 |
+
- Current date is {datetime.now().strftime('%d-%m-%Y')}
|
| 84 |
+
- If it's a relative date (tomorrow, next week), calculate accordingly
|
| 85 |
+
- If it's ambiguous, choose the next occurrence
|
| 86 |
+
- Return the date in DD-MM-YYYY format or "INVALID"
|
| 87 |
+
"""),
|
| 88 |
+
("human", "Please provide the interpreted date.")
|
| 89 |
+
])
|
| 90 |
+
|
| 91 |
+
final_response = (final_prompt | self.llm).invoke({})
|
| 92 |
+
final_date = final_response.content.strip()
|
| 93 |
+
|
| 94 |
+
if final_date != "INVALID":
|
| 95 |
+
try:
|
| 96 |
+
return datetime.strptime(final_date, "%d-%m-%Y")
|
| 97 |
+
except ValueError:
|
| 98 |
+
pass
|
| 99 |
+
|
| 100 |
+
return None
|
| 101 |
+
|
| 102 |
+
def validate_date(self, date_string: str, timezone_str: str = None) -> Tuple[bool, str, Optional[str]]:
|
| 103 |
+
"""Validate the parsed date."""
|
| 104 |
+
date_string = re.sub(r'\s+', ' ', date_string.strip())
|
| 105 |
+
|
| 106 |
+
parsed_date = self.parse_date_string(date_string)
|
| 107 |
+
if not parsed_date:
|
| 108 |
+
return False, "", "I couldn't understand that date format. Please provide a date like '7 March' or 'next Monday'."
|
| 109 |
+
|
| 110 |
+
current_date = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)
|
| 111 |
+
if parsed_date.replace(tzinfo=timezone.utc) < current_date:
|
| 112 |
+
return False, "", "The date cannot be in the past. Please provide a future date."
|
| 113 |
+
|
| 114 |
+
max_future_date = current_date + timedelta(days=730)
|
| 115 |
+
if parsed_date.replace(tzinfo=timezone.utc) > max_future_date:
|
| 116 |
+
return False, "", "The date cannot be more than 2 years in the future."
|
| 117 |
+
|
| 118 |
+
formatted_date = parsed_date.strftime("%d-%m-%Y")
|
| 119 |
+
|
| 120 |
+
return True, formatted_date, None
|
| 121 |
+
|
| 122 |
+
def get_phase_dates(self, phase_start_date: str, timezone_str: str) -> Dict[str, str]:
|
| 123 |
+
|
| 124 |
+
tz = pytz.timezone(timezone_str)
|
| 125 |
+
|
| 126 |
+
phase_start = datetime.strptime(phase_start_date, "%d-%m-%Y")
|
| 127 |
+
phase_start = tz.localize(phase_start.replace(hour=0, minute=0, second=0, microsecond=0))
|
| 128 |
+
|
| 129 |
+
phase_end = phase_start + timedelta(days=15)
|
| 130 |
+
|
| 131 |
+
drill_registration_start = datetime.now(tz).replace(microsecond=0)
|
| 132 |
+
|
| 133 |
+
drill_registration_end = phase_start - timedelta(days=1)
|
| 134 |
+
|
| 135 |
+
return {
|
| 136 |
+
"phaseStartDt": phase_start.strftime("%d-%m-%Y"),
|
| 137 |
+
"phaseEndDt": phase_end.strftime("%d-%m-%Y"),
|
| 138 |
+
"drillRegistrationStartDt": drill_registration_start.strftime("%d-%m-%Y"),
|
| 139 |
+
"drillRegistrationEndDt": drill_registration_end.strftime("%d-%m-%Y")
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
def get_timezone_offset(self, timezone_str: str) -> str:
|
| 143 |
+
|
| 144 |
+
tz = pytz.timezone(timezone_str)
|
| 145 |
+
offset = tz.utcoffset(datetime.now())
|
| 146 |
+
hours = int(offset.total_seconds() // 3600)
|
| 147 |
+
minutes = int((offset.total_seconds() % 3600) // 60)
|
| 148 |
+
return f"{'+' if hours >= 0 else '-'}{abs(hours):02d}:{minutes:02d}"
|
src/utils/llm_utils.py
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import json
|
| 3 |
+
from typing import List, Tuple, Optional, Dict
|
| 4 |
+
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate
|
| 5 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
| 6 |
+
|
| 7 |
+
from .core import get_llm, llm
|
| 8 |
+
|
| 9 |
+
def infer_purpose(state: dict, user_response: str, llm=None) -> str:
|
| 10 |
+
"""Use LLM to infer the purpose of the event."""
|
| 11 |
+
if llm is None:
|
| 12 |
+
llm = get_llm()
|
| 13 |
+
subcategory = state["hackathon_details"].get("drillSubCategory", "").upper()
|
| 14 |
+
prompt = f"""
|
| 15 |
+
Based on the drill subcategory '{subcategory}' and the user input: '{user_response}',
|
| 16 |
+
determine the most likely purpose of the event.
|
| 17 |
+
The possible purposes are 'Innovation' or 'Hiring'.
|
| 18 |
+
If the input strongly suggests one of these purposes, return it.
|
| 19 |
+
Otherwise, default to 'Innovation'.
|
| 20 |
+
"""
|
| 21 |
+
prompt_template = ChatPromptTemplate.from_messages([
|
| 22 |
+
("system", prompt),
|
| 23 |
+
("human", "Provide the inferred purpose.")
|
| 24 |
+
])
|
| 25 |
+
response = (prompt_template | llm).invoke({})
|
| 26 |
+
inferred_purpose = response.content.strip().capitalize()
|
| 27 |
+
return inferred_purpose if inferred_purpose in ["Innovation", "Hiring"] else "Innovation"
|
| 28 |
+
|
| 29 |
+
def infer_drill_type(user_response: str, llm=None) -> str:
|
| 30 |
+
"""Use LLM to infer the drill type as 'Theme Based' or 'Problem Based'."""
|
| 31 |
+
if llm is None:
|
| 32 |
+
llm = get_llm()
|
| 33 |
+
|
| 34 |
+
prompt = f"""
|
| 35 |
+
Based on the user input: '{user_response}', determine whether the drill type should be 'Theme Based' or 'Problem Based'.
|
| 36 |
+
|
| 37 |
+
- If the input contains keywords like 'theme', 'topic', or 'thematic', infer 'Theme Based'.
|
| 38 |
+
- If the input contains words like 'problem', 'challenge', or 'issue', infer 'Problem Based'.
|
| 39 |
+
- If it's unclear, default to 'Problem Based'.
|
| 40 |
+
|
| 41 |
+
Respond with only 'Theme Based' or 'Problem Based'.
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
response = llm.invoke([SystemMessage(content=prompt), HumanMessage(content="Infer the drill type.")])
|
| 45 |
+
inferred_drill_type = response.content.strip()
|
| 46 |
+
|
| 47 |
+
return inferred_drill_type if inferred_drill_type in ["Theme Based", "Problem Based"] else "Problem Based"
|
| 48 |
+
|
| 49 |
+
def infer_subcategory(user_response: str, category_subcategory_map: dict, llm=None) -> str:
|
| 50 |
+
"""Use LLM to infer the most relevant subcategory."""
|
| 51 |
+
if llm is None:
|
| 52 |
+
llm = get_llm()
|
| 53 |
+
valid_subcategories = list(category_subcategory_map.keys())
|
| 54 |
+
prompt = f"""
|
| 55 |
+
Given the user input: '{user_response}', determine the most relevant subcategory from the following list:
|
| 56 |
+
{valid_subcategories}.
|
| 57 |
+
If no clear match is found, return an empty string.
|
| 58 |
+
"""
|
| 59 |
+
prompt_template = ChatPromptTemplate.from_messages([
|
| 60 |
+
("system", prompt),
|
| 61 |
+
("human", "Provide the inferred subcategory.")
|
| 62 |
+
])
|
| 63 |
+
response = (prompt_template | llm).invoke({})
|
| 64 |
+
inferred_subcategory = response.content.strip().upper()
|
| 65 |
+
return inferred_subcategory if inferred_subcategory in category_subcategory_map else ""
|
| 66 |
+
|
| 67 |
+
def infer_yes_no(user_response: str, llm=None) -> str:
|
| 68 |
+
"""Use LLM to infer whether the user's response is 'Yes' or 'No'."""
|
| 69 |
+
if llm is None:
|
| 70 |
+
llm = get_llm()
|
| 71 |
+
prompt = f"""
|
| 72 |
+
Given the user input: '{user_response}', determine whether the response indicates 'Yes' or 'No'.
|
| 73 |
+
Return 'Yes' if the input strongly suggests affirmation, otherwise return 'No'.
|
| 74 |
+
"""
|
| 75 |
+
prompt_template = ChatPromptTemplate.from_messages([
|
| 76 |
+
("system", prompt),
|
| 77 |
+
("human", "Provide the inferred response.")
|
| 78 |
+
])
|
| 79 |
+
response = (prompt_template | llm).invoke({})
|
| 80 |
+
inferred_response = response.content.strip().capitalize()
|
| 81 |
+
return inferred_response if inferred_response in ["Yes", "No"] else "No"
|
| 82 |
+
|
| 83 |
+
def infer_user_uncertainty(user_input: str, llm=None) -> bool:
|
| 84 |
+
"""Detect if user is uncertain about naming their event."""
|
| 85 |
+
if llm is None:
|
| 86 |
+
llm = get_llm()
|
| 87 |
+
prompt = f"""
|
| 88 |
+
Analyze this user input for signs of uncertainty about naming: "{user_input}"
|
| 89 |
+
|
| 90 |
+
Look for:
|
| 91 |
+
1. Direct expressions ("I'm not sure", "I don't know")
|
| 92 |
+
2. Questions ("what should I name it?", "can you help?")
|
| 93 |
+
3. Hesitation ("maybe", "possibly")
|
| 94 |
+
4. Requests for suggestions
|
| 95 |
+
5. General uncertainty
|
| 96 |
+
|
| 97 |
+
Return ONLY "True" if uncertain, "False" if confident.
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
response = llm.invoke([
|
| 101 |
+
SystemMessage(content=prompt),
|
| 102 |
+
HumanMessage(content="Is the user uncertain about naming?")
|
| 103 |
+
])
|
| 104 |
+
return response.content.strip().lower() == "true"
|
| 105 |
+
|
| 106 |
+
def infer_eligibility(user_response: str, llm=None) -> List[str]:
|
| 107 |
+
"""Infer which categories of participants are eligible for an event."""
|
| 108 |
+
if llm is None:
|
| 109 |
+
llm = get_llm()
|
| 110 |
+
|
| 111 |
+
valid_categories = ["Graduate", "Working Professionals", "College Students"]
|
| 112 |
+
|
| 113 |
+
user_response_lower = user_response.lower().strip()
|
| 114 |
+
|
| 115 |
+
if any(term in user_response_lower for term in ["all", "everyone", "anybody", "anyone"]):
|
| 116 |
+
return valid_categories
|
| 117 |
+
|
| 118 |
+
matched_categories = []
|
| 119 |
+
|
| 120 |
+
if "graduate" in user_response_lower or "graduates" in user_response_lower:
|
| 121 |
+
matched_categories.append("Graduate")
|
| 122 |
+
|
| 123 |
+
if "college" in user_response_lower or "students" in user_response_lower:
|
| 124 |
+
matched_categories.append("College Students")
|
| 125 |
+
|
| 126 |
+
if "working" in user_response_lower or "professional" in user_response_lower or "professionals" in user_response_lower:
|
| 127 |
+
matched_categories.append("Working Professionals")
|
| 128 |
+
|
| 129 |
+
if matched_categories:
|
| 130 |
+
return matched_categories
|
| 131 |
+
|
| 132 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 133 |
+
("system", """Analyze the user's response and determine which categories of participants are eligible for an event.
|
| 134 |
+
|
| 135 |
+
Valid categories:
|
| 136 |
+
- Graduate
|
| 137 |
+
- College Students
|
| 138 |
+
- Working Professionals
|
| 139 |
+
|
| 140 |
+
Instructions:
|
| 141 |
+
1. Return ONLY the valid categories that match the user's response
|
| 142 |
+
2. Be specific - only include categories that are clearly mentioned or implied
|
| 143 |
+
3. If the user mentions "students" without further specification, interpret as "College Students"
|
| 144 |
+
4. If the user mentions "professionals" or "working people", interpret as "Working Professionals"
|
| 145 |
+
5. If the user mentions "graduates", interpret as "Graduate"
|
| 146 |
+
6. If the intent is unclear, return the most likely category based on the response
|
| 147 |
+
7. Return the result as a JSON array with exact category names as shown above
|
| 148 |
+
|
| 149 |
+
Examples:
|
| 150 |
+
- "Students and professionals" → ["College Students", "Working Professionals"]
|
| 151 |
+
- "Only graduates" → ["Graduate"]
|
| 152 |
+
- "Anyone with a degree" → ["Graduate", "Working Professionals"]
|
| 153 |
+
- "Students in college" → ["College Students"]
|
| 154 |
+
"""),
|
| 155 |
+
("human", user_response)
|
| 156 |
+
])
|
| 157 |
+
|
| 158 |
+
try:
|
| 159 |
+
response = (prompt | llm).invoke({})
|
| 160 |
+
|
| 161 |
+
if response is None or not hasattr(response, 'content') or not response.content:
|
| 162 |
+
print("LLM returned null or empty response")
|
| 163 |
+
return ["College Students"]
|
| 164 |
+
|
| 165 |
+
content = response.content.strip()
|
| 166 |
+
|
| 167 |
+
if not content:
|
| 168 |
+
print("LLM returned empty content")
|
| 169 |
+
return ["College Students"]
|
| 170 |
+
|
| 171 |
+
categories = []
|
| 172 |
+
try:
|
| 173 |
+
if "[" in content and "]" in content:
|
| 174 |
+
start = content.find("[")
|
| 175 |
+
end = content.rfind("]") + 1
|
| 176 |
+
json_str = content[start:end]
|
| 177 |
+
parsed_content = json.loads(json_str)
|
| 178 |
+
if parsed_content is None:
|
| 179 |
+
return ["College Students"]
|
| 180 |
+
categories = parsed_content
|
| 181 |
+
else:
|
| 182 |
+
parsed_content = json.loads(content)
|
| 183 |
+
if parsed_content is None:
|
| 184 |
+
return ["College Students"]
|
| 185 |
+
categories = parsed_content
|
| 186 |
+
except json.JSONDecodeError:
|
| 187 |
+
cleaned = content.replace("The eligible categories are:", "")
|
| 188 |
+
cleaned = cleaned.replace("Based on the response, the eligible categories are:", "")
|
| 189 |
+
if "," in cleaned:
|
| 190 |
+
categories = [cat.strip() for cat in cleaned.split(",")]
|
| 191 |
+
elif "\n" in cleaned:
|
| 192 |
+
categories = [cat.strip() for cat in cleaned.split("\n") if cat.strip()]
|
| 193 |
+
else:
|
| 194 |
+
categories = [cleaned.strip()]
|
| 195 |
+
|
| 196 |
+
if not categories:
|
| 197 |
+
print("Failed to extract categories from LLM response")
|
| 198 |
+
return ["College Students"]
|
| 199 |
+
|
| 200 |
+
validated_categories = []
|
| 201 |
+
for cat in categories:
|
| 202 |
+
if cat is None or not isinstance(cat, str) or not cat.strip():
|
| 203 |
+
continue
|
| 204 |
+
|
| 205 |
+
matches = [valid for valid in valid_categories if valid.lower() == cat.lower()]
|
| 206 |
+
if matches:
|
| 207 |
+
validated_categories.append(matches[0])
|
| 208 |
+
elif "college" in cat.lower() or "student" in cat.lower():
|
| 209 |
+
if "College Students" not in validated_categories:
|
| 210 |
+
validated_categories.append("College Students")
|
| 211 |
+
elif "work" in cat.lower() or "professional" in cat.lower():
|
| 212 |
+
if "Working Professionals" not in validated_categories:
|
| 213 |
+
validated_categories.append("Working Professionals")
|
| 214 |
+
elif "grad" in cat.lower():
|
| 215 |
+
if "Graduate" not in validated_categories:
|
| 216 |
+
validated_categories.append("Graduate")
|
| 217 |
+
|
| 218 |
+
return validated_categories if validated_categories else ["College Students"]
|
| 219 |
+
|
| 220 |
+
except Exception as e:
|
| 221 |
+
print(f"Error in infer_eligibility: {str(e)}")
|
| 222 |
+
return ["College Students"]
|
src/utils/partner_utils.py
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import json
|
| 4 |
+
import requests
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from typing import Optional, Dict
|
| 7 |
+
from bs4 import BeautifulSoup
|
| 8 |
+
from selenium import webdriver
|
| 9 |
+
from selenium.webdriver.chrome.service import Service
|
| 10 |
+
from selenium.webdriver.chrome.options import Options
|
| 11 |
+
from webdriver_manager.chrome import ChromeDriverManager
|
| 12 |
+
import time
|
| 13 |
+
|
| 14 |
+
from .core import get_llm
|
| 15 |
+
|
| 16 |
+
def scrape_website(url: str, wait_time: int = 10) -> str:
|
| 17 |
+
"""
|
| 18 |
+
Scrapes a website using Selenium and returns the HTML content.
|
| 19 |
+
"""
|
| 20 |
+
chrome_options = Options()
|
| 21 |
+
chrome_options.add_argument("--headless")
|
| 22 |
+
chrome_options.add_argument("--no-sandbox")
|
| 23 |
+
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 24 |
+
|
| 25 |
+
driver = webdriver.Chrome(
|
| 26 |
+
service=Service(ChromeDriverManager().install()),
|
| 27 |
+
options=chrome_options
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
driver.get(url)
|
| 32 |
+
time.sleep(wait_time)
|
| 33 |
+
return driver.page_source
|
| 34 |
+
finally:
|
| 35 |
+
driver.quit()
|
| 36 |
+
|
| 37 |
+
def analyze_with_llm(html_content: str, url: str, llm) -> dict:
|
| 38 |
+
"""
|
| 39 |
+
Process the HTML content with the LLM to extract partner information.
|
| 40 |
+
"""
|
| 41 |
+
prompt = f"""
|
| 42 |
+
Analyze the following HTML content and extract information about the partner or company.
|
| 43 |
+
Return ONLY a JSON object of the following format,all the defined fields are mandatory, with no additional text:
|
| 44 |
+
|
| 45 |
+
{{
|
| 46 |
+
"active": true,
|
| 47 |
+
"customUrl": "<domain-with-dashes-without-the-.com>",
|
| 48 |
+
"partnerDisplayName": "<Readable name of the partner>",
|
| 49 |
+
"partnerEstablishmentDate": "<year of establishment of the company or when the company waas founded>",
|
| 50 |
+
"partnerLogoPath": "<URL of the partner's logo>",
|
| 51 |
+
"partnerName": "<Official Company Name>",
|
| 52 |
+
"partnerType": <type of industry inferred from the llm in all caps>,
|
| 53 |
+
"partnerDescription": "<A concise and informative description of the partner's business or activities>",
|
| 54 |
+
"partnerIndustry": "<Specific industry category the partner operates in>",
|
| 55 |
+
"partnerSocialLinks": "{{\\\"websiteurl\\\": \\\"https://www.example.com/\\\"}}"
|
| 56 |
+
// include other social media links like twitter instagram linkedin if present
|
| 57 |
+
}}
|
| 58 |
+
|
| 59 |
+
IMPORTANT: For the "partnerSocialLinks" field, it should be a JSON string (not an object).
|
| 60 |
+
For example: "partnerSocialLinks": "{{\\\"websiteurl\\\": \\\"https://www.example.com/\\\"}}", do the same for other social links if found
|
| 61 |
+
|
| 62 |
+
If a specific piece of information is not available in the HTML, use an empty string for that field.
|
| 63 |
+
Make sure to return valid JSON that can be parsed by Python's json module.
|
| 64 |
+
|
| 65 |
+
HTML CONTENT:
|
| 66 |
+
{html_content}
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
response = llm.invoke(prompt)
|
| 70 |
+
response_text = response.content
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
if "```json" in response_text and "```" in response_text.split("```json")[1]:
|
| 74 |
+
json_content = response_text.split("```json")[1].split("```")[0].strip()
|
| 75 |
+
elif "```" in response_text and "```" in response_text.split("```")[1]:
|
| 76 |
+
json_content = response_text.split("```")[1].split("```")[0].strip()
|
| 77 |
+
else:
|
| 78 |
+
json_content = response_text.strip()
|
| 79 |
+
|
| 80 |
+
return json.loads(json_content)
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"Error parsing JSON from LLM response: {e}")
|
| 84 |
+
domain = url.split('/')[2] if len(url.split('/')) > 2 else url
|
| 85 |
+
domain = domain.replace("www.", "")
|
| 86 |
+
|
| 87 |
+
return {
|
| 88 |
+
"active": True,
|
| 89 |
+
"customUrl": domain.replace(".", "-"),
|
| 90 |
+
"partnerDisplayName": domain.split('.')[0].title(),
|
| 91 |
+
"partnerEstablishmentDate": str(datetime.now().year),
|
| 92 |
+
"partnerLogoPath": "NA",
|
| 93 |
+
"partnerName": domain.split('.')[0].title(),
|
| 94 |
+
"partnerType": "INDUSTRY",
|
| 95 |
+
"partnerDescription": "Technology company",
|
| 96 |
+
"partnerIndustry": "Technology",
|
| 97 |
+
"partnerSocialLinks": json.dumps({"websiteurl": url})
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
def extract_partner_info(url: str, llm=None) -> dict:
|
| 101 |
+
"""
|
| 102 |
+
Extracts partner information from a given URL using Selenium and LLM analysis.
|
| 103 |
+
"""
|
| 104 |
+
if llm is None:
|
| 105 |
+
llm = get_llm()
|
| 106 |
+
|
| 107 |
+
try:
|
| 108 |
+
# Normalize URL
|
| 109 |
+
if not url.startswith(('http://', 'https://')):
|
| 110 |
+
url = f'https://{url}'
|
| 111 |
+
|
| 112 |
+
# Scrape website using Selenium
|
| 113 |
+
html_content = scrape_website(url)
|
| 114 |
+
|
| 115 |
+
# Analyze content with LLM
|
| 116 |
+
partner_info = analyze_with_llm(html_content, url, llm)
|
| 117 |
+
|
| 118 |
+
return partner_info
|
| 119 |
+
|
| 120 |
+
except Exception as e:
|
| 121 |
+
print(f"Error in extract_partner_info: {str(e)}")
|
| 122 |
+
domain = url.split('/')[2] if len(url.split('/')) > 2 else url
|
| 123 |
+
domain = domain.replace("www.", "")
|
| 124 |
+
return {
|
| 125 |
+
"active": True,
|
| 126 |
+
"customUrl": domain.replace(".", "-"),
|
| 127 |
+
"partnerDisplayName": domain.split('.')[0].title(),
|
| 128 |
+
"partnerEstablishmentDate": str(datetime.now().year),
|
| 129 |
+
"partnerLogoPath": "NA",
|
| 130 |
+
"partnerName": domain.split('.')[0].title(),
|
| 131 |
+
"partnerType": "INDUSTRY",
|
| 132 |
+
"partnerDescription": "Technology company",
|
| 133 |
+
"partnerIndustry": "Technology",
|
| 134 |
+
"partnerSocialLinks": json.dumps({"websiteurl": url})
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
def get_existing_partner(partner_name: str) -> Optional[dict]:
|
| 138 |
+
"""Find if partner already exists in the system."""
|
| 139 |
+
api_url = os.getenv("PARTNER_URL")
|
| 140 |
+
|
| 141 |
+
try:
|
| 142 |
+
normalized_name = partner_name.lower().strip()
|
| 143 |
+
|
| 144 |
+
response = requests.get(f"{api_url}?name={partner_name}")
|
| 145 |
+
response.raise_for_status()
|
| 146 |
+
partners = response.json()
|
| 147 |
+
|
| 148 |
+
if partners and isinstance(partners, list):
|
| 149 |
+
for partner in partners:
|
| 150 |
+
if partner.get("partnerName", "").lower().strip() == normalized_name:
|
| 151 |
+
return partner
|
| 152 |
+
|
| 153 |
+
for partner in partners:
|
| 154 |
+
if normalized_name in partner.get("partnerName", "").lower() or \
|
| 155 |
+
partner.get("partnerName", "").lower() in normalized_name:
|
| 156 |
+
return partner
|
| 157 |
+
|
| 158 |
+
domain_match = re.search(r'(?:https?://)?(?:www\.)?([^/]+)', partner_name)
|
| 159 |
+
if domain_match:
|
| 160 |
+
domain = domain_match.group(1)
|
| 161 |
+
response = requests.get(f"{api_url}")
|
| 162 |
+
response.raise_for_status()
|
| 163 |
+
all_partners = response.json()
|
| 164 |
+
|
| 165 |
+
if all_partners and isinstance(all_partners, list):
|
| 166 |
+
for partner in all_partners:
|
| 167 |
+
partner_links = partner.get("partnerSocialLinks", "{}")
|
| 168 |
+
if isinstance(partner_links, str):
|
| 169 |
+
try:
|
| 170 |
+
links_dict = json.loads(partner_links)
|
| 171 |
+
website = links_dict.get("websiteurl", "")
|
| 172 |
+
if domain in website:
|
| 173 |
+
return partner
|
| 174 |
+
except json.JSONDecodeError:
|
| 175 |
+
pass
|
| 176 |
+
|
| 177 |
+
return None
|
| 178 |
+
except Exception as e:
|
| 179 |
+
print(f"Error checking existing partner: {str(e)}")
|
| 180 |
+
return None
|
| 181 |
+
|
| 182 |
+
def register_partner(partner_info: dict) -> dict:
|
| 183 |
+
"""Register a new partner in the system."""
|
| 184 |
+
|
| 185 |
+
api_url = os.getenv("PARTNER_URL")
|
| 186 |
+
headers = {
|
| 187 |
+
"Content-Type": "application/json",
|
| 188 |
+
"Accept": "application/json"
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
try:
|
| 192 |
+
response = requests.post(api_url, json=partner_info, headers=headers)
|
| 193 |
+
response.raise_for_status()
|
| 194 |
+
return response.json()
|
| 195 |
+
except Exception as e:
|
| 196 |
+
print(f"\n!!! Error in register_partner: {str(e)}")
|
| 197 |
+
raise
|
| 198 |
+
|
| 199 |
+
def validate_partner_url(url: str) -> bool:
|
| 200 |
+
"""
|
| 201 |
+
Validates if a given URL is accessible and returns a valid response.
|
| 202 |
+
"""
|
| 203 |
+
try:
|
| 204 |
+
if not url.startswith(('http://', 'https://')):
|
| 205 |
+
url = f'https://{url}'
|
| 206 |
+
|
| 207 |
+
session = requests.Session()
|
| 208 |
+
session.headers.update({
|
| 209 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 210 |
+
})
|
| 211 |
+
|
| 212 |
+
response = session.get(
|
| 213 |
+
url,
|
| 214 |
+
timeout=15,
|
| 215 |
+
verify=False,
|
| 216 |
+
allow_redirects=True
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
# Check if response is successful and contains HTML content
|
| 220 |
+
return (response.status_code == 200 and
|
| 221 |
+
'text/html' in response.headers.get('Content-Type', '').lower())
|
| 222 |
+
|
| 223 |
+
except Exception as e:
|
| 224 |
+
print(f"URL validation error: {str(e)}")
|
| 225 |
+
return False
|
| 226 |
+
|
| 227 |
+
def find_partner_by_url(url: str) -> Optional[dict]:
|
| 228 |
+
"""
|
| 229 |
+
Find a partner by URL in the social links
|
| 230 |
+
"""
|
| 231 |
+
api_url = os.getenv("PARTNER_URL")
|
| 232 |
+
|
| 233 |
+
try:
|
| 234 |
+
# Normalize the URL for comparison
|
| 235 |
+
if not url.startswith(('http://', 'https://')):
|
| 236 |
+
url = f'https://{url}'
|
| 237 |
+
|
| 238 |
+
# Remove trailing slash if present
|
| 239 |
+
url = url.rstrip('/')
|
| 240 |
+
|
| 241 |
+
# Get all partners
|
| 242 |
+
response = requests.get(api_url)
|
| 243 |
+
response.raise_for_status()
|
| 244 |
+
partners = response.json()
|
| 245 |
+
|
| 246 |
+
if not partners or not isinstance(partners, list):
|
| 247 |
+
return None
|
| 248 |
+
|
| 249 |
+
# First try exact URL match
|
| 250 |
+
for partner in partners:
|
| 251 |
+
social_links = partner.get("partnerSocialLinks", "{}")
|
| 252 |
+
if isinstance(social_links, str):
|
| 253 |
+
try:
|
| 254 |
+
links_dict = json.loads(social_links)
|
| 255 |
+
website = links_dict.get("websiteurl", "").rstrip('/')
|
| 256 |
+
|
| 257 |
+
# Try different variations of the URL
|
| 258 |
+
url_variations = [
|
| 259 |
+
url,
|
| 260 |
+
url.replace("https://", "http://"),
|
| 261 |
+
url.replace("http://", "https://"),
|
| 262 |
+
url.replace("www.", ""),
|
| 263 |
+
url + "/"
|
| 264 |
+
]
|
| 265 |
+
|
| 266 |
+
if website and any(website == variation for variation in url_variations):
|
| 267 |
+
return partner
|
| 268 |
+
except json.JSONDecodeError:
|
| 269 |
+
pass
|
| 270 |
+
|
| 271 |
+
# Try domain match if exact match fails
|
| 272 |
+
domain = url.split('/')[2] if len(url.split('/')) > 2 else url
|
| 273 |
+
domain = domain.replace("www.", "")
|
| 274 |
+
|
| 275 |
+
for partner in partners:
|
| 276 |
+
social_links = partner.get("partnerSocialLinks", "{}")
|
| 277 |
+
if isinstance(social_links, str):
|
| 278 |
+
try:
|
| 279 |
+
links_dict = json.loads(social_links)
|
| 280 |
+
website = links_dict.get("websiteurl", "")
|
| 281 |
+
if domain in website or domain.split('.')[0] in website:
|
| 282 |
+
return partner
|
| 283 |
+
except json.JSONDecodeError:
|
| 284 |
+
pass
|
| 285 |
+
|
| 286 |
+
print(f"No partner found for URL: {url}")
|
| 287 |
+
return None
|
| 288 |
+
except Exception as e:
|
| 289 |
+
print(f"Error finding partner by URL: {str(e)}")
|
| 290 |
+
return None
|