File size: 7,070 Bytes
4b88321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# modal_app.py - Modal deployment for ResearchCopilot
import modal
import os
from pathlib import Path

# Create Modal app
app = modal.App("research-copilot")

# Define the environment with required packages
image = modal.Image.debian_slim(python_version="3.11").pip_install([
    "gradio>=4.0.0",
    "httpx",
    "aiohttp",
    "python-dotenv",
    "requests",
    "beautifulsoup4",
    "openai",  # For potential LLM integrations
    "anthropic",  # For Claude integration
])

# Mount the application code
code_mount = modal.Mount.from_local_dir(
    ".", 
    remote_path="/app",
    condition=lambda path: path.suffix in [".py", ".txt", ".md"]
)

@app.function(
    image=image,
    mounts=[code_mount],
    allow_concurrent_inputs=100,
    timeout=3600,  # 1 hour timeout for long research tasks
    secrets=[
        modal.Secret.from_name("research-copilot-secrets"),  # API keys
    ]
)
@modal.web_server(port=7860, startup_timeout=60)
def run_gradio_app():
    """Run the ResearchCopilot Gradio application"""
    import sys
    sys.path.append("/app")
    
    # Import and run the main application
    from ResearchCopilot.research_copilot import create_interface
    
    app = create_interface()
    app.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,  # Modal handles the sharing
        show_error=True,
        enable_queue=True
    )

# Enhanced retriever with real API integrations
@app.function(
    image=image,
    secrets=[modal.Secret.from_name("research-copilot-secrets")],
    timeout=300
)
async def search_perplexity(query: str, num_results: int = 5):
    """Search using Perplexity API"""
    import httpx
    import os
    
    api_key = os.getenv("PERPLEXITY_API_KEY")
    if not api_key:
        # Return mock data if no API key
        return {
            "results": [
                {
                    "title": f"Mock Result for: {query}",
                    "url": "https://example.com/mock",
                    "snippet": f"This is a mock result for the query: {query}",
                    "source_type": "web"
                }
            ]
        }
    
    async with httpx.AsyncClient() as client:
        try:
            response = await client.post(
                "https://api.perplexity.ai/chat/completions",
                headers={
                    "Authorization": f"Bearer {api_key}",
                    "Content-Type": "application/json"
                },
                json={
                    "model": "llama-3.1-sonar-small-128k-online",
                    "messages": [
                        {"role": "user", "content": f"Search for: {query}"}
                    ],
                    "max_tokens": 1000,
                    "temperature": 0.2,
                    "return_citations": True
                }
            )
            
            if response.status_code == 200:
                data = response.json()
                return {"results": data.get("choices", [{}])[0].get("message", {}).get("content", "")}
            else:
                return {"error": f"API error: {response.status_code}"}
                
        except Exception as e:
            return {"error": str(e)}

@app.function(
    image=image,
    secrets=[modal.Secret.from_name("research-copilot-secrets")],
    timeout=300
)
async def search_google(query: str, num_results: int = 10):
    """Search using Google Custom Search API"""
    import httpx
    import os
    
    api_key = os.getenv("GOOGLE_API_KEY")
    search_engine_id = os.getenv("GOOGLE_SEARCH_ENGINE_ID")
    
    if not api_key or not search_engine_id:
        # Return mock data if no API keys
        return {
            "results": [
                {
                    "title": f"Google Search: {query}",
                    "url": "https://example.com/google-mock",
                    "snippet": f"Mock Google search result for: {query}",
                    "source_type": "web"
                }
            ]
        }
    
    async with httpx.AsyncClient() as client:
        try:
            response = await client.get(
                "https://www.googleapis.com/customsearch/v1",
                params={
                    "key": api_key,
                    "cx": search_engine_id,
                    "q": query,
                    "num": min(num_results, 10)
                }
            )
            
            if response.status_code == 200:
                data = response.json()
                results = []
                for item in data.get("items", []):
                    results.append({
                        "title": item.get("title", ""),
                        "url": item.get("link", ""),
                        "snippet": item.get("snippet", ""),
                        "source_type": "web"
                    })
                return {"results": results}
            else:
                return {"error": f"Google API error: {response.status_code}"}
                
        except Exception as e:
            return {"error": str(e)}

@app.function(
    image=image,
    secrets=[modal.Secret.from_name("research-copilot-secrets")],
    timeout=600
)
async def summarize_with_claude(content: str, context: str = ""):
    """Summarize content using Claude API"""
    import httpx
    import os
    
    api_key = os.getenv("ANTHROPIC_API_KEY")
    if not api_key:
        # Return mock summary if no API key
        return {
            "summary": f"Mock summary of content: {content[:100]}...",
            "key_points": ["Point 1", "Point 2", "Point 3"]
        }
    
    async with httpx.AsyncClient() as client:
        try:
            response = await client.post(
                "https://api.anthropic.com/v1/messages",
                headers={
                    "x-api-key": api_key,
                    "Content-Type": "application/json",
                    "anthropic-version": "2023-06-01"
                },
                json={
                    "model": "claude-3-sonnet-20240229",
                    "max_tokens": 1000,
                    "messages": [
                        {
                            "role": "user",
                            "content": f"Summarize this content and extract key points:\n\nContext: {context}\n\nContent: {content}"
                        }
                    ]
                }
            )
            
            if response.status_code == 200:
                data = response.json()
                content_text = data.get("content", [{}])[0].get("text", "")
                return {
                    "summary": content_text,
                    "key_points": ["AI-generated summary", "Professional analysis", "Comprehensive overview"]
                }
            else:
                return {"error": f"Claude API error: {response.status_code}"}
                
        except Exception as e:
            return {"error": str(e)}

if __name__ == "__main__":
    # For local development
    import subprocess
    subprocess.run(["python", "research_copilot.py"])