File size: 2,193 Bytes
eac7939
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import modal
from pathlib import Path
import sys

# Current directory path
src_dir = Path(__file__).parent / "src"
agent_dir = src_dir / "yt_agent"
gradio_dir = src_dir / "yt_gradio"
rag_dir = src_dir / "yt_rag"

# Create Modal image with required dependencies
web_image = modal.Image.debian_slim(python_version="3.10").pip_install(
    "python-dotenv",
    "fastapi[standard]==0.115.4",
    "gradio[mcp]==5.33.0",
    "requests",
    "smolagents",
    # Add any other dependencies you need
).add_local_file(rag_dir / "rag.py", "/root/src/yt_rag/rag.py") \
.add_local_file(gradio_dir / "app.py", "/root/src/yt_gradio/app.py") \
.add_local_file(agent_dir / "agent.py", "/root/src/yt_agent/agent.py") \
.add_local_file(agent_dir / "prompts.py", "/root/src/yt_agent/prompts.py") \
.add_local_file(agent_dir / "tools.py", "/root/src/yt_agent/tools.py") \
.add_local_file(src_dir / "schemas.py", "/root/src/schemas.py")

app = modal.App("youtwo-gradio", image=web_image)

# Modal limits
MAX_CONCURRENT_USERS = 10
MINUTES = 60  # seconds
TIME_LIMIT = 10 * MINUTES  # time limit (3540 seconds, just under the 3600s maximum)

# This volume will store any local files needed by the app
volume = modal.Volume.from_name("youtwo-volume", create_if_missing=True)

@app.function(
    volumes={"/data": volume},
    secrets=[
        modal.Secret.from_name("vectara"),
        modal.Secret.from_name("nebius")
    ],
    max_containers=1,
    scaledown_window=TIME_LIMIT,  # 3540 seconds, within the allowed 2-3600 second range
)
@modal.asgi_app()
def gradio_app():
    from fastapi import FastAPI
    from gradio.routes import mount_gradio_app
    
    # Add /root to path for imports
    sys.path.append("/root")
    
    # Import RAG functions
    # from rag import is_allowed_filetype, upload_file_to_vectara, retrieve_chunks
    from src.yt_gradio.app import get_gradio_blocks
    
    # ---------------------------
    # Backend Functions
    # ---------------------------
    
    blocks = get_gradio_blocks()    # Mount Gradio app to FastAPI for Modal
    app = FastAPI()
    return mount_gradio_app(app=app, blocks=blocks, path="/")


if __name__ == "__main__":
    gradio_app.serve(mcp_server=True)