Sench729 commited on
Commit
38980bb
·
1 Parent(s): 979c76c

deploy my fast api to hugging face space

Browse files
.dockerignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ __pycache__
2
+ *.pyc
3
+ *.pyo
4
+ *.pyd
5
+ .env
6
+ venv/
7
+ .git
8
+ .DS_Store
.env.example ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ GEMINI_API_KEY="Your Gemini API key"
2
+ SERPER_API_KEY="Your Serper API key"
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .env
2
+ eventhub.egg-info/*
3
+ __pycache__/
Dockerfile ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use official Python image
2
+ FROM python:3.11-slim
3
+
4
+ # Set environment variables
5
+ ENV PYTHONDONTWRITEBYTECODE=1
6
+ ENV PYTHONUNBUFFERED=1
7
+
8
+ # Set working directory
9
+ WORKDIR /app
10
+
11
+ # Copy requirements first for caching
12
+ COPY requirements.txt .
13
+
14
+ # Install dependencies
15
+ RUN pip install --upgrade pip
16
+ RUN pip install --no-cache-dir -r requirements.txt
17
+
18
+ # Copy app code
19
+ COPY . .
20
+
21
+ # Expose port (Render uses 10000+ dynamic ports, we'll override with CMD)
22
+ EXPOSE 7860
23
+
24
+ # Redirect local storage and cache to /tmp
25
+ ENV HOME=/tmp
26
+ ENV XDG_DATA_HOME=/tmp/.local/share
27
+
28
+ # Command to run the FastAPI server
29
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from pydantic import BaseModel
4
+ import uvicorn
5
+ import logging
6
+ import threading
7
+ import time
8
+ import asyncio
9
+ from typing import Dict, Optional
10
+ import os
11
+ # Import core components
12
+ from components.search.search import perform_search, get_all_events, initialize_embeddings, refresh_embeddings
13
+ from components.Event_ai.crew import EventContentCrew
14
+
15
+ # Import MCP server
16
+ from mcp_server import SimpleMCPServer
17
+
18
+ # -----------------------------
19
+ # Logging
20
+ # -----------------------------
21
+ logger = logging.getLogger(__name__)
22
+ logging.basicConfig(
23
+ level=logging.INFO,
24
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
25
+ )
26
+
27
+ # -----------------------------
28
+ # FastAPI app
29
+ # -----------------------------
30
+ app = FastAPI(
31
+ title="EventHub API",
32
+ description="Semantic Event Search, Event Creation, and AI-assisted tools API",
33
+ version="1.1.0",
34
+ docs_url="/docs"
35
+ )
36
+
37
+ # -----------------------------
38
+ # Middleware
39
+ # -----------------------------
40
+ app.add_middleware(
41
+ CORSMiddleware,
42
+ allow_origins=["https://grad-backend-uaju.onrender.com/"],
43
+ allow_credentials=True,
44
+ allow_methods=["*"],
45
+ allow_headers=["*"],
46
+ )
47
+
48
+ _mcp_server_instance: Optional[SimpleMCPServer] = None
49
+ _mcp_server_started = False
50
+
51
+ # -----------------------------
52
+ # Models
53
+ # -----------------------------
54
+ class SearchRequest(BaseModel):
55
+ query: str
56
+ user_id: str = "default"
57
+
58
+ class EventInput(BaseModel):
59
+ description: str
60
+ title: str
61
+
62
+ # -----------------------------
63
+ # Global state tracking
64
+ # -----------------------------
65
+ _mcp_server_started = False
66
+ _mcp_server_instance: Optional[SimpleMCPServer] = None
67
+
68
+ # -----------------------------
69
+ # Background task for periodic refresh
70
+ # -----------------------------
71
+ def periodic_refresh():
72
+ """Refresh embeddings every 30 minutes"""
73
+ logger.info("→ Starting periodic refresh thread")
74
+
75
+ while True:
76
+ try:
77
+ time.sleep(1800) # 30 minutes
78
+ logger.info("→ Starting periodic embedding refresh...")
79
+ refresh_embeddings()
80
+ logger.info("✓ Embeddings refreshed successfully")
81
+ except Exception as e:
82
+ logger.error(f"✗ Error during periodic refresh: {e}")
83
+
84
+ # -----------------------------
85
+ # Startup
86
+ # -----------------------------
87
+ @app.on_event("startup")
88
+ async def startup_event():
89
+ global _mcp_server_started, _mcp_server_instance
90
+
91
+ logger.info("→ Starting EventHub application")
92
+
93
+ # Initialize embeddings asynchronously
94
+ try:
95
+ logger.info("→ Initializing embeddings...")
96
+ loop = asyncio.get_event_loop()
97
+ await loop.run_in_executor(None, initialize_embeddings)
98
+ logger.info("✓ Embeddings initialized successfully")
99
+ except Exception as e:
100
+ logger.error(f"✗ Error initializing embeddings: {e}")
101
+ logger.warning("→ Continuing without embeddings (search functionality may be limited)")
102
+
103
+ # Start background refresh thread
104
+ try:
105
+ refresh_thread = threading.Thread(target=periodic_refresh, daemon=True)
106
+ refresh_thread.start()
107
+ logger.info("✓ Background refresh thread started")
108
+ except Exception as e:
109
+ logger.error(f"✗ Failed to start refresh thread: {e}")
110
+
111
+ # Launch MCP server instance
112
+ try:
113
+ if not _mcp_server_started:
114
+ logger.info("→ Starting MCP server instance...")
115
+ _mcp_server_instance = SimpleMCPServer()
116
+ _mcp_server_started = True
117
+ logger.info("✓ MCP server instance ready")
118
+ except Exception as e:
119
+ logger.error(f"✗ Failed to start MCP server: {e}")
120
+
121
+ # -----------------------------
122
+ # Routes
123
+ # -----------------------------
124
+ @app.get("/")
125
+ async def health_check():
126
+ return {"status": "healthy", "message": "EventHub API is running"}
127
+
128
+ @app.post("/search")
129
+ async def search_endpoint(request: SearchRequest):
130
+ try:
131
+ results = perform_search(request.query, request.user_id)
132
+ if not results:
133
+ return {"results": [], "message": "No matching events found"}
134
+ return {"results": results}
135
+ except Exception as e:
136
+ logger.error(f"Search endpoint error: {e}")
137
+ raise HTTPException(status_code=500, detail="Search failed")
138
+
139
+ @app.post("/generate-event")
140
+ def generate_event(event: EventInput):
141
+ try:
142
+ crew = EventContentCrew()
143
+ result = crew.eventcrew().kickoff(
144
+ inputs={"event_description": event.description, "title": event.title}
145
+ )
146
+
147
+ # Access tasks_output safely
148
+ tasks = getattr(result, "tasks_output", [])
149
+ if tasks:
150
+ last_task = tasks[-1]
151
+ # Use vars() to convert TaskOutput object to dict
152
+ last_task_dict = vars(last_task) if hasattr(last_task, "__dict__") else {}
153
+ proposals = last_task_dict.get("json_dict", {}).get("proposals", [])
154
+ else:
155
+ proposals = getattr(result, "proposals", [])
156
+
157
+ return {"proposals": proposals}
158
+
159
+ except Exception as e:
160
+ logger.error(f"Generate-event error: {e}")
161
+ raise HTTPException(status_code=500, detail="Event generation failed")
162
+
163
+
164
+ @app.post("/refresh-embeddings")
165
+ async def manual_refresh():
166
+ try:
167
+ collection = refresh_embeddings()
168
+ return {"status": "success", "message": "Embeddings refreshed successfully","collection": collection}
169
+ except Exception as e:
170
+ logger.error(f"Manual refresh error: {e}")
171
+ raise HTTPException(status_code=500, detail="Embedding refresh failed")
172
+
173
+
174
+ # -----------------------------
175
+ # MCP HTTP Bridge Routes
176
+ # -----------------------------
177
+ @app.post("/mcp/tools/call")
178
+ async def mcp_tools_call(request: Dict):
179
+ global _mcp_server_instance
180
+
181
+ if not _mcp_server_instance:
182
+ logger.error("✗ MCP server instance not available")
183
+ raise HTTPException(status_code=503, detail="MCP server not initialized")
184
+
185
+ logger.info(f"→ MCP bridge request: {request.get('method', 'unknown')}")
186
+
187
+ try:
188
+ response = await _mcp_server_instance.handle_request(request)
189
+ logger.info("✓ MCP bridge request completed")
190
+ return response
191
+ except Exception as e:
192
+ logger.error(f"✗ MCP bridge error: {e}")
193
+ raise HTTPException(status_code=500, detail=f"MCP request failed: {str(e)}")
194
+
195
+ @app.post("/mcp/tools/list")
196
+ async def mcp_tools_list():
197
+ global _mcp_server_instance
198
+
199
+ if not _mcp_server_instance:
200
+ logger.error("✗ MCP server instance not available")
201
+ raise HTTPException(status_code=503, detail="MCP server not initialized")
202
+
203
+ logger.info("→ MCP tools list request")
204
+
205
+ try:
206
+ request = {"jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {}}
207
+ response = await _mcp_server_instance.handle_request(request)
208
+ logger.info("✓ MCP tools list completed")
209
+ return response
210
+ except Exception as e:
211
+ logger.error(f"✗ MCP tools list error: {e}")
212
+ raise HTTPException(status_code=500, detail=f"MCP tools list failed: {str(e)}")
213
+
214
+ if __name__ == "__main__":
215
+ logger.info("→ Starting EventHub server on http://localhost:8000")
216
+ logger.info("→ API documentation available at http://localhost:8000/docs")
217
+
218
+ try:
219
+ port = int(os.getenv("PORT", 8000))
220
+ uvicorn.run(app, host="0.0.0.0", port=port, reload=True)
221
+ except Exception as e:
222
+ logger.error(f"✗ Failed to start server: {e}")
223
+ raise
224
+
components/Event_ai/__init__.py ADDED
File without changes
components/Event_ai/config/__init__.py ADDED
File without changes
components/Event_ai/config/agents.yaml ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ event_generator:
2
+ name: "Event Content Generator"
3
+ role: "Event copywriter"
4
+ goal: "Generate 3 polished event titles, short descriptions (≤500 words), and relevant tags."
5
+ backstory: "You are an expert in event branding and writing catchy content."
6
+ allow_delegation: false
7
+
8
+ event_validator:
9
+ name: "Event Validator"
10
+ role: "Validator"
11
+ goal: "Ensure the generated proposals align with the event description."
12
+ backstory: "You are a meticulous event reviewer ensuring relevance and accuracy."
13
+ allow_delegation: false
components/Event_ai/config/tasks.yaml ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ generate_event_content:
2
+ description: >
3
+ Generate up to 3 polished event proposals (title, description ≤500 words, and 5 tags)
4
+ for the following event:
5
+ description:{event_description}.
6
+ title: {title}
7
+
8
+ Instructions for the LLM:
9
+ - If the event description mentions any guest, special guest, or speaker,
10
+ Use SerperDevTool to search online (Google, LinkedIn, Wikipedia).
11
+ Summarize their role or profession using LLM.
12
+ include them in the generated description as bullet points.
13
+ - For each guest/speaker, provide their name and role (e.g., CEO of a company,
14
+ software developer, market analyst, musician, or related relevant role).
15
+ - Keep the description engaging and concise.
16
+ - Ensure each proposal has:
17
+ 1. A catchy title
18
+ 2. Polished description ≤500 words
19
+ 3. 5 relevant tags
20
+ - Generate up to 3 proposals for the given event description.
21
+
22
+ expected_output: >
23
+ JSON object with key "proposals", containing a list of up to 3 event proposals.
24
+ Each proposal must include:
25
+ - title (string)
26
+ - description (string ≤500 words, including bullet points for guests/speakers if any)
27
+ - tags (list of 5 strings)
28
+
29
+ validate_event_content:
30
+ description: >
31
+ From the generated proposals, analyze and return the 2 that best align
32
+ with the original event description: {event_description}.
33
+ expected_output: >
34
+ JSON object with key "proposals", containing a list of the 2 best validated event proposals.
components/Event_ai/crew.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+ from crewai import Agent, Crew, Process, Task, LLM
3
+ from crewai.project import CrewBase, agent, task, crew
4
+ from crewai_tools import SerperDevTool, ScrapeWebsiteTool
5
+
6
+ from pydantic import BaseModel, Field
7
+ from dotenv import load_dotenv
8
+
9
+ _ = load_dotenv()
10
+
11
+ llm = LLM(
12
+ model="gemini/gemini-2.0-flash",
13
+ temperature=0.7,
14
+ )
15
+
16
+ class EventProposal(BaseModel):
17
+ title: str = Field(..., description="The event title")
18
+ description: str = Field(..., description="Event description, max 200 words")
19
+ tags: List[str] = Field(..., description="List of 5 suggested tags")
20
+
21
+
22
+ class EventProposals(BaseModel):
23
+ proposals: List[EventProposal] = Field(..., description="List of event proposals")
24
+
25
+
26
+ @CrewBase
27
+ class EventContentCrew:
28
+ """Crew for generating and validating event proposals"""
29
+
30
+ agents_config = "config/agents.yaml"
31
+ tasks_config = "config/tasks.yaml"
32
+
33
+ @agent
34
+ def event_generator(self) -> Agent:
35
+ return Agent(
36
+ config=self.agents_config["event_generator"],
37
+ llm=llm,
38
+ inject_date=True,
39
+ tools=[
40
+ SerperDevTool()
41
+ ]
42
+ )
43
+
44
+ @agent
45
+ def event_validator(self) -> Agent:
46
+ return Agent(
47
+ config=self.agents_config["event_validator"],
48
+ llm=llm,
49
+ inject_date=True,
50
+ )
51
+
52
+ @task
53
+ def generate_event_content(self) -> Task:
54
+ return Task(
55
+ config=self.tasks_config["generate_event_content"],
56
+ agent=self.event_generator(),
57
+ output_json=EventProposals
58
+ )
59
+
60
+ @task
61
+ def validate_event_content(self) -> Task:
62
+ return Task(
63
+ config=self.tasks_config["validate_event_content"],
64
+ agent=self.event_validator(),
65
+ output_json=EventProposals
66
+ )
67
+
68
+
69
+ @crew
70
+ def eventcrew(self) -> Crew:
71
+ """Creates the event crew"""
72
+ return Crew(
73
+ agents=self.agents,
74
+ tasks=self.tasks,
75
+ process=Process.sequential,
76
+ verbose=True,
77
+ planning=False,
78
+ max_rpm=3,
79
+ )
components/__init__.py ADDED
File without changes
components/search/__init__.py ADDED
File without changes
components/search/search.py ADDED
@@ -0,0 +1,311 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ from datetime import datetime, timedelta
3
+ import re
4
+ import numpy as np
5
+ import os
6
+ import json
7
+ from sentence_transformers import SentenceTransformer
8
+ import chromadb
9
+ from chromadb.config import Settings
10
+ import requests
11
+ from events_data import events
12
+
13
+ # -----------------------------
14
+ # Logging
15
+ # -----------------------------
16
+ logger = logging.getLogger(__name__)
17
+
18
+ # -----------------------------
19
+ # Configuration
20
+ # -----------------------------
21
+ PERSISTENT_DIRECTORY = "./chroma_db"
22
+ EVENTS_API_URL = "http://localhost:3000/api/allevents"
23
+
24
+ EMBEDDINGS_CACHE_FILE = "./embeddings_cache.json"
25
+
26
+ # -----------------------------
27
+ # Global collection variable
28
+ # -----------------------------
29
+ collection = None
30
+
31
+ # -----------------------------
32
+ # Initialize model
33
+ # -----------------------------
34
+ try:
35
+ model = SentenceTransformer('all-MiniLM-L6-v2')
36
+ logger.info("SentenceTransformer model loaded successfully")
37
+ except Exception as e:
38
+ logger.error(f"Error loading model: {e}")
39
+ raise
40
+
41
+ # -----------------------------
42
+ # Initialize ChromaDB with persistent storage
43
+ # -----------------------------
44
+ def get_chroma_client():
45
+ """Get ChromaDB client with persistent storage"""
46
+ return chromadb.Client(Settings(
47
+ persist_directory=PERSISTENT_DIRECTORY
48
+ ))
49
+
50
+ def initialize_collection():
51
+ """Initialize or get existing collection"""
52
+ client = get_chroma_client()
53
+ try:
54
+ collection = client.get_collection(name="event_content")
55
+ logger.info("Using existing ChromaDB collection")
56
+ except:
57
+ collection = client.create_collection(name="event_content")
58
+ logger.info("Created new ChromaDB collection")
59
+ return collection
60
+
61
+ # -----------------------------
62
+ # Sample events (fallback)
63
+ # -----------------------------
64
+ sample_events = events
65
+
66
+ # -----------------------------
67
+ # Embedding management functions
68
+ # -----------------------------
69
+ def initialize_embeddings():
70
+ """Initialize embeddings on application startup"""
71
+ global collection
72
+ collection = initialize_collection()
73
+
74
+ # Check if collection is empty and needs initial population
75
+ if collection.count() == 0:
76
+ logger.info("Collection empty, generating initial embeddings...")
77
+ refresh_embeddings()
78
+ else:
79
+ logger.info(f"Collection already has {collection.count()} embeddings")
80
+
81
+ def refresh_embeddings():
82
+ """Refresh embeddings by recreating the collection"""
83
+ global collection
84
+
85
+ # Get current events (all events, no user filtering at storage level)
86
+ events = fetch_events()
87
+
88
+ # Recreate the collection
89
+ try:
90
+ client = get_chroma_client()
91
+ try:
92
+ client.delete_collection(name="event_content")
93
+ logger.info("Deleted existing collection")
94
+ except:
95
+ logger.info("No existing collection to delete")
96
+
97
+ collection = client.create_collection(name="event_content")
98
+ logger.info("Created new collection")
99
+
100
+ except Exception as e:
101
+ logger.error(f"Error recreating collection: {e}")
102
+ raise
103
+
104
+ # Generate new embeddings (store all events with user_id as "global")
105
+ documents = []
106
+ metadatas = []
107
+ ids = []
108
+
109
+ for event in events:
110
+ search_text = generate_search_text(event)
111
+ documents.append(search_text)
112
+
113
+ # Store all events with user_id as "global" - filtering happens during search
114
+ metadatas.append({
115
+ "user_id": "global", # All events stored as global
116
+ "_id": str(event["_id"]),
117
+ "title": event.get("title", ""),
118
+ "location": event.get("location") or "",
119
+ "createdAt": event.get("createdAt") or "",
120
+ "imageUrl": event.get("imageUrl") or "",
121
+ "startDateTime": event.get("startDateTime") or "",
122
+ "endDateTime": event.get("endDateTime") or "",
123
+ "price": event.get("price") or "",
124
+ "isFree": event.get("isFree", False),
125
+ "category": event.get("category", {}).get("name", ""),
126
+ "organizer": f"{event.get('organizer', {}).get('firstName','')} {event.get('organizer', {}).get('lastName','')}".strip(),
127
+ "tags": ", ".join(event.get("tags", []))
128
+ })
129
+
130
+ ids.append(f"global_{event['_id']}")
131
+
132
+ try:
133
+ # Process in batches to manage memory
134
+ batch_size = 20
135
+ for i in range(0, len(documents), batch_size):
136
+ batch_docs = documents[i:i+batch_size]
137
+ batch_metadatas = metadatas[i:i+batch_size]
138
+ batch_ids = ids[i:i+batch_size]
139
+
140
+ batch_embeddings = model.encode(batch_docs).tolist()
141
+
142
+ collection.add(
143
+ documents=batch_docs,
144
+ embeddings=batch_embeddings,
145
+ metadatas=batch_metadatas,
146
+ ids=batch_ids
147
+ )
148
+ logger.info(f"Processed batch {i//batch_size + 1}/{(len(documents)-1)//batch_size + 1}")
149
+
150
+ logger.info(f"Embeddings refreshed successfully for {len(events)} events")
151
+ return events
152
+
153
+ except Exception as e:
154
+ logger.error(f"Error refreshing embeddings: {e}")
155
+ raise
156
+
157
+ def generate_search_text(event):
158
+ """Generate search text from event data"""
159
+ return (
160
+ f"Title: {event['title']}. "
161
+ f"Description: {event.get('description', '')}. "
162
+ f"Category: {event.get('category', {}).get('name', '')}. "
163
+ f"Location: {event.get('location', '')}. "
164
+ f"Tags: {', '.join(event.get('tags', []))}. "
165
+ f"Organizer: {event.get('organizer', {}).get('firstName', '')} {event.get('organizer', {}).get('lastName', '')}. "
166
+ f"Start: {event.get('startDateTime', '')}. "
167
+ f"End: {event.get('endDateTime', '')}. "
168
+ f"Price: {event.get('price', '')}. "
169
+ f"Free: {'Yes' if event.get('isFree', False) else 'No'}. "
170
+ f"URL: {event.get('url', '')}"
171
+ )
172
+
173
+ def fetch_events():
174
+ # """Fetch events from API with fallback to sample data"""
175
+ try:
176
+ response = requests.get(EVENTS_API_URL, timeout=10)
177
+ response.raise_for_status()
178
+ events = response.json()
179
+ logger.info(f"Fetched {len(events)} events from API")
180
+ # print("Events: ",events)
181
+ return events['data']
182
+ except Exception as e:
183
+ logger.warning(f"Error fetching events from API, using sample data: {e}")
184
+ return sample_events
185
+
186
+ # -----------------------------
187
+ # Helper: extract filters
188
+ # -----------------------------
189
+ def extract_filters_from_query(query: str):
190
+ filters = {}
191
+
192
+ # Free / Paid
193
+ if "free" in query.lower():
194
+ filters["isFree"] = True
195
+ query = re.sub(r"\bfree\b", "", query, flags=re.IGNORECASE)
196
+ if "paid" in query.lower():
197
+ filters["isFree"] = False
198
+ query = re.sub(r"\bpaid\b", "", query, flags=re.IGNORECASE)
199
+
200
+ # Location detection
201
+ location_match = re.search(r"in\s+([A-Za-z\s]+)", query, re.IGNORECASE)
202
+ if location_match:
203
+ filters["location"] = location_match.group(1).strip().title()
204
+ query = re.sub(location_match.group(0), "", query, flags=re.IGNORECASE)
205
+
206
+ # Date detection
207
+ today = datetime.now().date()
208
+ date_patterns = {
209
+ "today": today,
210
+ "tomorrow": today + timedelta(days=1),
211
+ "this weekend": today + timedelta(days=(5 - today.weekday()) % 7),
212
+ "next week": today + timedelta(days=7),
213
+ }
214
+ for pattern, date_value in date_patterns.items():
215
+ if pattern in query.lower():
216
+ filters["date"] = date_value.strftime("%Y-%m-%d")
217
+ query = re.sub(pattern, "", query, flags=re.IGNORECASE)
218
+
219
+ # Category detection
220
+ categories = ["technology", "music", "business", "sports", "education",
221
+ "food & drink", "gaming", "health & wellness"]
222
+ for category in categories:
223
+ if category in query.lower():
224
+ filters["category"] = category.title()
225
+ query = re.sub(category, "", query, flags=re.IGNORECASE)
226
+
227
+ query = re.sub(r'\s+', ' ', query).strip()
228
+ return query, filters
229
+
230
+ # -----------------------------
231
+ # Main search function
232
+ # -----------------------------
233
+ def perform_search(query: str, user_id: str):
234
+ n_results = 10
235
+
236
+ similarity_threshold = 0.2
237
+
238
+
239
+ try:
240
+ if collection is None:
241
+ initialize_embeddings()
242
+
243
+ processed_query, filters = extract_filters_from_query(query)
244
+
245
+ # Base filter: only return global events
246
+ where_filter = {"user_id": "global"}
247
+
248
+ # Add other filters from query parsing
249
+ other_filters = []
250
+ if "isFree" in filters:
251
+ other_filters.append({"isFree": filters["isFree"]})
252
+ if "location" in filters:
253
+ other_filters.append({"location": filters["location"]})
254
+ if "category" in filters:
255
+ other_filters.append({"category": filters["category"]})
256
+
257
+ if other_filters:
258
+ where_filter = {
259
+ "$and": [
260
+ {"user_id": "global"},
261
+ *other_filters
262
+ ]
263
+ }
264
+
265
+ # Generate query embedding
266
+ query_embedding = model.encode([processed_query]).tolist()[0]
267
+
268
+ # Perform the search
269
+ results = collection.query(
270
+ query_embeddings=[query_embedding],
271
+ n_results=n_results,
272
+ where=where_filter,
273
+ include=['metadatas', 'distances']
274
+ )
275
+
276
+ metadatas = results.get("metadatas", [[]])[0]
277
+ distances = results.get("distances", [[]])[0]
278
+
279
+ if not metadatas:
280
+ return []
281
+
282
+ # Convert distances to similarity scores
283
+ scored_results = []
284
+ for idx, metadata in enumerate(metadatas):
285
+ distance = distances[idx]
286
+ similarity_score = 1 / (1 + distance) if distance > 0 else 1.0
287
+
288
+ # ✅ filter by threshold
289
+ if similarity_score >= similarity_threshold:
290
+ scored_results.append({
291
+ "metadata": metadata,
292
+ "score": similarity_score
293
+ })
294
+
295
+ # If nothing meets threshold → return empty
296
+ if not scored_results:
297
+ return []
298
+
299
+ # Sort and return
300
+ scored_results.sort(key=lambda x: x["score"], reverse=True)
301
+ return [r["metadata"] for r in scored_results]
302
+
303
+ except Exception as e:
304
+ logger.error(f"Search error for user {user_id}: {e}")
305
+ raise
306
+
307
+ # -----------------------------
308
+ # Return all events
309
+ # -----------------------------
310
+ def get_all_events():
311
+ return fetch_events()
events_data.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ events = [
2
+ {
3
+ "_id": "1",
4
+ "title": "Tech Conference 2023",
5
+ "description": "Annual technology conference featuring talks on AI, machine learning, and software development. Join industry leaders for three days of innovation.",
6
+ "tags": ["technology", "AI", "machine learning", "software", "conference"],
7
+ "location": "San Francisco, CA",
8
+ "createdAt": "2023-08-01T00:00:00",
9
+ "imageUrl": "https://example.com/event1.jpg",
10
+ "startDateTime": "2023-11-15T09:00:00",
11
+ "endDateTime": "2023-11-15T17:00:00",
12
+ "price": "TBD",
13
+ "isFree": False,
14
+ "url": "https://example.com/events/1",
15
+ "category": {"_id": "c1", "name": "Technology"},
16
+ "organizer": {"_id": "o1", "firstName": "John", "lastName": "Doe"}
17
+ },
18
+ {
19
+ "_id": "2",
20
+ "title": "Jazz Festival",
21
+ "description": "Weekend jazz festival with performances from world-renowned artists. Food and drinks available throughout the event.",
22
+ "tags": ["music", "jazz", "festival", "live performance", "food"],
23
+ "location": "New York, NY",
24
+ "createdAt": "2023-08-02T00:00:00",
25
+ "imageUrl": "https://example.com/event2.jpg",
26
+ "startDateTime": "2023-09-22T10:00:00",
27
+ "endDateTime": "2023-09-22T22:00:00",
28
+ "price": "50",
29
+ "isFree": False,
30
+ "url": "https://example.com/events/2",
31
+ "category": {"_id": "c2", "name": "Music"},
32
+ "organizer": {"_id": "o2", "firstName": "Alice", "lastName": "Smith"}
33
+ },
34
+ {
35
+ "_id": "3",
36
+ "title": "Startup Pitch Competition",
37
+ "description": "Watch promising startups pitch their ideas to a panel of investors. Great opportunity for networking with entrepreneurs.",
38
+ "tags": ["business", "startup", "pitching", "investors", "networking"],
39
+ "location": "Austin, TX",
40
+ "createdAt": "2023-08-03T00:00:00",
41
+ "imageUrl": "https://example.com/event3.jpg",
42
+ "startDateTime": "2023-10-05T14:00:00",
43
+ "endDateTime": "2023-10-05T18:00:00",
44
+ "price": "0",
45
+ "isFree": True,
46
+ "url": "https://example.com/events/3",
47
+ "category": {"_id": "c3", "name": "Business"},
48
+ "organizer": {"_id": "o3", "firstName": "Bob", "lastName": "Johnson"}
49
+ },
50
+ {
51
+ "_id": "4",
52
+ "title": "Marathon for Charity",
53
+ "description": "Annual marathon raising funds for children's hospitals. All fitness levels welcome with 5K, 10K, and full marathon options.",
54
+ "tags": ["sports", "marathon", "charity", "fitness", "running"],
55
+ "location": "Chicago, IL",
56
+ "createdAt": "2023-08-04T00:00:00",
57
+ "imageUrl": "https://example.com/event4.jpg",
58
+ "startDateTime": "2023-10-28T07:00:00",
59
+ "endDateTime": "2023-10-28T13:00:00",
60
+ "price": "25",
61
+ "isFree": False,
62
+ "url": "https://example.com/events/4",
63
+ "category": {"_id": "c4", "name": "Sports"},
64
+ "organizer": {"_id": "o4", "firstName": "Sarah", "lastName": "Lee"}
65
+ },
66
+ {
67
+ "_id": "5",
68
+ "title": "Digital Marketing Workshop",
69
+ "description": "Hands-on workshop teaching the latest digital marketing strategies, SEO techniques, and social media advertising.",
70
+ "tags": ["education", "marketing", "SEO", "social media", "workshop"],
71
+ "location": "Online Event",
72
+ "createdAt": "2023-08-05T00:00:00",
73
+ "imageUrl": "https://example.com/event5.jpg",
74
+ "startDateTime": "2023-09-30T09:00:00",
75
+ "endDateTime": "2023-09-30T16:00:00",
76
+ "price": "0",
77
+ "isFree": True,
78
+ "url": "https://example.com/events/5",
79
+ "category": {"_id": "c5", "name": "Education"},
80
+ "organizer": {"_id": "o5", "firstName": "David", "lastName": "Kim"}
81
+ },
82
+ {
83
+ "_id": "6",
84
+ "title": "Food & Wine Festival",
85
+ "description": "Sample gourmet foods and fine wines from top chefs and vineyards. Cooking demonstrations and tasting sessions throughout the day.",
86
+ "tags": ["food", "wine", "festival", "cooking", "tasting"],
87
+ "location": "Napa Valley, CA",
88
+ "createdAt": "2023-08-06T00:00:00",
89
+ "imageUrl": "https://example.com/event6.jpg",
90
+ "startDateTime": "2023-11-10T11:00:00",
91
+ "endDateTime": "2023-11-10T20:00:00",
92
+ "price": "75",
93
+ "isFree": False,
94
+ "url": "https://example.com/events/6",
95
+ "category": {"_id": "c6", "name": "Food & Drink"},
96
+ "organizer": {"_id": "o6", "firstName": "Emma", "lastName": "Williams"}
97
+ },
98
+ {
99
+ "_id": "7",
100
+ "title": "VR Gaming Expo",
101
+ "description": "Experience the latest in virtual reality gaming technology. Try new games, meet developers, and participate in tournaments.",
102
+ "tags": ["gaming", "VR", "virtual reality", "expo", "tournaments"],
103
+ "location": "Los Angeles, CA",
104
+ "createdAt": "2023-08-07T00:00:00",
105
+ "imageUrl": "https://example.com/event7.jpg",
106
+ "startDateTime": "2023-10-14T10:00:00",
107
+ "endDateTime": "2023-10-14T18:00:00",
108
+ "price": "30",
109
+ "isFree": False,
110
+ "url": "https://example.com/events/7",
111
+ "category": {"_id": "c7", "name": "Gaming"},
112
+ "organizer": {"_id": "o7", "firstName": "Liam", "lastName": "Brown"}
113
+ },
114
+ {
115
+ "_id": "8",
116
+ "title": "Yoga & Wellness Retreat",
117
+ "description": "Weekend retreat focusing on yoga, meditation, and holistic wellness practices. Suitable for all experience levels.",
118
+ "tags": ["health", "wellness", "yoga", "meditation", "retreat"],
119
+ "location": "Sedona, AZ",
120
+ "createdAt": "2023-08-08T00:00:00",
121
+ "imageUrl": "https://example.com/event8.jpg",
122
+ "startDateTime": "2023-11-05T08:00:00",
123
+ "endDateTime": "2023-11-05T17:00:00",
124
+ "price": "100",
125
+ "isFree": False,
126
+ "url": "https://example.com/events/8",
127
+ "category": {"_id": "c8", "name": "Health & Wellness"},
128
+ "organizer": {"_id": "o8", "firstName": "Sophia", "lastName": "Martinez"}
129
+ },
130
+ {
131
+ "_id": "9",
132
+ "title": "Tech Meetup: AI Applications",
133
+ "description": "Monthly meetup discussing practical applications of artificial intelligence in various industries. Networking session included.",
134
+ "tags": ["technology", "AI", "meetup", "networking", "applications"],
135
+ "location": "San Francisco, CA",
136
+ "createdAt": "2023-08-09T00:00:00",
137
+ "imageUrl": "https://example.com/event9.jpg",
138
+ "startDateTime": "2023-10-12T18:00:00",
139
+ "endDateTime": "2023-10-12T21:00:00",
140
+ "price": "0",
141
+ "isFree": True,
142
+ "url": "https://example.com/events/9",
143
+ "category": {"_id": "c9", "name": "Technology"},
144
+ "organizer": {"_id": "o9", "firstName": "Olivia", "lastName": "Davis"}
145
+ },
146
+ {
147
+ "_id": "10",
148
+ "title": "Blockchain Conference",
149
+ "description": "Exploring the future of blockchain technology, cryptocurrencies, and decentralized applications. Features industry experts.",
150
+ "tags": ["technology", "blockchain", "crypto", "conference", "decentralized"],
151
+ "location": "Austin, TX",
152
+ "createdAt": "2023-08-10T00:00:00",
153
+ "imageUrl": "https://example.com/event10.jpg",
154
+ "startDateTime": "2023-11-20T09:00:00",
155
+ "endDateTime": "2023-11-20T17:00:00",
156
+ "price": "120",
157
+ "isFree": False,
158
+ "url": "https://example.com/events/10",
159
+ "category": {"_id": "c10", "name": "Technology"},
160
+ "organizer": {"_id": "o10", "firstName": "James", "lastName": "Miller"}
161
+ }
162
+ ]
mcp_server.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Simplified MCP Server for EventHub RAG functionality
3
+ Uses basic JSON-RPC over stdio instead of complex MCP libraries
4
+ """
5
+
6
+ import asyncio
7
+ import json
8
+ import sys
9
+ import os
10
+ from typing import Any, Dict
11
+
12
+ sys.path.append('.')
13
+ from components.search.search import perform_search, get_all_events, initialize_embeddings
14
+
15
+ class SimpleMCPServer:
16
+ def __init__(self):
17
+ self.tools = {
18
+ "search_events": {
19
+ "name": "search_events",
20
+ "description": "Search for events using natural language query with filters",
21
+ "inputSchema": {
22
+ "type": "object",
23
+ "properties": {
24
+ "query": {"type": "string", "description": "Natural language query for events"},
25
+ "user_id": {"type": "string", "description": "User identifier", "default": "default"}
26
+ },
27
+ "required": ["query"]
28
+ }
29
+ },
30
+ "get_all_events": {
31
+ "name": "get_all_events",
32
+ "description": "Get all available events without filtering",
33
+ "inputSchema": {"type": "object", "properties": {}}
34
+ }
35
+ }
36
+
37
+ async def handle_request(self, request: Dict[str, Any]) -> Dict[str, Any]:
38
+ """Handle incoming JSON-RPC request"""
39
+ try:
40
+ method = request.get("method")
41
+ params = request.get("params", {})
42
+ request_id = request.get("id")
43
+
44
+ if method == "tools/list":
45
+ result = {"tools": list(self.tools.values())}
46
+
47
+ elif method == "tools/call":
48
+ tool_name = params.get("name")
49
+ tool_args = params.get("arguments", {})
50
+
51
+ if tool_name == "search_events":
52
+ result = await self.search_events(tool_args)
53
+ elif tool_name == "get_all_events":
54
+ result = await self.get_all_events(tool_args)
55
+ else:
56
+ raise ValueError(f"Unknown tool: {tool_name}")
57
+
58
+ else:
59
+ raise ValueError(f"Unknown method: {method}")
60
+
61
+ return {
62
+ "jsonrpc": "2.0",
63
+ "id": request_id,
64
+ "result": result
65
+ }
66
+
67
+ except Exception as e:
68
+ return {
69
+ "jsonrpc": "2.0",
70
+ "id": request.get("id"),
71
+ "error": {"code": -1, "message": str(e)}
72
+ }
73
+
74
+ async def search_events(self, args: Dict[str, Any]) -> Dict[str, Any]:
75
+ """Handle search_events tool call"""
76
+ query = args.get("query", "")
77
+ user_id = args.get("user_id", "default")
78
+
79
+ try:
80
+ results = perform_search(query, user_id)
81
+ formatted_results = []
82
+ for event in results:
83
+ formatted_event = {
84
+ "title": event.get("title", ""),
85
+ "category": event.get("category", ""),
86
+ "location": event.get("location", ""),
87
+ "startDateTime": event.get("startDateTime", ""),
88
+ "endDateTime": event.get("endDateTime", ""),
89
+ "price": event.get("price", ""),
90
+ "isFree": event.get("isFree", False),
91
+ "organizer": event.get("organizer", ""),
92
+ "tags": event.get("tags", "")
93
+ }
94
+ formatted_results.append(formatted_event)
95
+
96
+ response_data = {
97
+ "query": query,
98
+ "results_count": len(formatted_results),
99
+ "events": formatted_results[:10]
100
+ }
101
+
102
+ return {
103
+ "content": [{
104
+ "type": "text",
105
+ "text": json.dumps(response_data, indent=2)
106
+ }]
107
+ }
108
+
109
+ except Exception as e:
110
+ return {
111
+ "content": [{
112
+ "type": "text",
113
+ "text": f"Error searching events: {str(e)}"
114
+ }]
115
+ }
116
+
117
+ async def get_all_events(self, args: Dict[str, Any]) -> Dict[str, Any]:
118
+ """Handle get_all_events tool call"""
119
+ try:
120
+ events = get_all_events()
121
+ response_data = {
122
+ "total_events": len(events),
123
+ "events": events[:20]
124
+ }
125
+ return {
126
+ "content": [{
127
+ "type": "text",
128
+ "text": json.dumps(response_data, indent=2)
129
+ }]
130
+ }
131
+ except Exception as e:
132
+ return {
133
+ "content": [{
134
+ "type": "text",
135
+ "text": f"Error getting events: {str(e)}"
136
+ }]
137
+ }
138
+
139
+ async def run(self):
140
+ """Main server loop"""
141
+ print("✓ Simple MCP Server started", file=sys.stderr)
142
+
143
+ while True:
144
+ try:
145
+ line = sys.stdin.readline()
146
+ if not line:
147
+ break
148
+ request = json.loads(line.strip())
149
+ response = await self.handle_request(request)
150
+ print(json.dumps(response))
151
+ sys.stdout.flush()
152
+
153
+ except json.JSONDecodeError:
154
+ continue
155
+ except Exception as e:
156
+ error_response = {
157
+ "jsonrpc": "2.0",
158
+ "id": None,
159
+ "error": {"code": -1, "message": str(e)}
160
+ }
161
+ print(json.dumps(error_response))
162
+ sys.stdout.flush()
163
+
164
+ async def main():
165
+ """Run the simplified MCP server"""
166
+ try:
167
+ initialize_embeddings()
168
+ print("✓ Event embeddings initialized", file=sys.stderr)
169
+ except Exception as e:
170
+ print(f"✗ Failed to initialize embeddings: {e}", file=sys.stderr)
171
+ server = SimpleMCPServer()
172
+ await server.run()
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ sentence-transformers
4
+ chromadb
5
+ numpy
6
+ pydantic
7
+ python-dotenv
8
+ crewai
9
+ crewai[tools]
10
+ mcp
setup.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name="eventhub",
5
+ version="0.1.0",
6
+ description="AI Agent and Semantic Event Search using FastAPI and ChromaDB",
7
+ author="Sintayehu Bikila",
8
+ author_email="sench729@gmail.com",
9
+ packages=find_packages(include=["components", "components.*"]),
10
+ python_requires=">=3.8",
11
+ )