Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -147,32 +147,80 @@ SOURCE:
|
|
| 147 |
</source_material>
|
| 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 |
if __name__ == "__main__":
|
| 177 |
print("Starting FastMCP server on port 7860...")
|
| 178 |
|
|
|
|
| 147 |
</source_material>
|
| 148 |
"""
|
| 149 |
|
| 150 |
+
mcp = FastMCP("file-analyzer")
|
| 151 |
+
|
| 152 |
+
# Helper functions for different file types
|
| 153 |
+
async def process_pdf(file_bytes: bytes) -> str:
|
| 154 |
+
"""Process PDF file and extract text/info"""
|
| 155 |
+
pdf_file = io.BytesIO(file_bytes)
|
| 156 |
+
reader = PdfReader(pdf_file)
|
| 157 |
+
|
| 158 |
+
num_pages = len(reader.pages)
|
| 159 |
+
text = ""
|
| 160 |
+
for page in reader.pages:
|
| 161 |
+
text += page.extract_text()
|
| 162 |
+
|
| 163 |
+
return f"PDF Analysis:\n- Pages: {num_pages}\n- Text length: {len(text)} chars\n\nFirst 500 chars:\n{text[:500]}"
|
| 164 |
+
|
| 165 |
+
async def process_image(file_bytes: bytes) -> str:
|
| 166 |
+
"""Process image file"""
|
| 167 |
+
img = Image.open(io.BytesIO(file_bytes))
|
| 168 |
+
return f"Image Analysis:\n- Format: {img.format}\n- Size: {img.size}\n- Mode: {img.mode}"
|
| 169 |
+
|
| 170 |
+
async def process_text(file_bytes: bytes) -> str:
|
| 171 |
+
"""Process text file"""
|
| 172 |
+
try:
|
| 173 |
+
text = file_bytes.decode('utf-8')
|
| 174 |
+
lines = text.split('\n')
|
| 175 |
+
words = len(text.split())
|
| 176 |
+
return f"Text Analysis:\n- Lines: {len(lines)}\n- Words: {words}\n- Characters: {len(text)}\n\nFirst 500 chars:\n{text[:500]}"
|
| 177 |
+
except UnicodeDecodeError:
|
| 178 |
+
return "Error: File is not valid UTF-8 text"
|
| 179 |
+
|
| 180 |
+
@mcp.tool
|
| 181 |
+
async def analyze_file(
|
| 182 |
+
ctx: Context,
|
| 183 |
+
file: bytes,
|
| 184 |
+
file_type: Optional[Literal["text", "image", "pdf"]] = None
|
| 185 |
+
) -> str:
|
| 186 |
"""
|
| 187 |
+
Analyzes a file.
|
| 188 |
+
|
| 189 |
+
Args:
|
| 190 |
+
file: The file content
|
| 191 |
+
file_type: Type of file (text/image/pdf). If not provided, will ask user.
|
| 192 |
"""
|
| 193 |
+
# Basic info
|
| 194 |
+
info = f"Received file of size {len(file)} bytes.\n\n"
|
| 195 |
+
|
| 196 |
+
# If file_type not provided, elicit it from user
|
| 197 |
+
if file_type is None:
|
| 198 |
+
result = await ctx.elicit(
|
| 199 |
+
message="What is the file type?",
|
| 200 |
+
response_type=["text", "image", "pdf"]
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
if result.action == "accept":
|
| 204 |
+
file_type = result.data
|
| 205 |
+
else:
|
| 206 |
+
return "Elicitation cancelled by user"
|
| 207 |
+
|
| 208 |
+
# Route to appropriate processor
|
| 209 |
+
try:
|
| 210 |
+
if file_type == "pdf":
|
| 211 |
+
result = await process_pdf(file)
|
| 212 |
+
elif file_type == "image":
|
| 213 |
+
result = await process_image(file)
|
| 214 |
+
elif file_type == "text":
|
| 215 |
+
result = await process_text(file)
|
| 216 |
+
else:
|
| 217 |
+
return info + f"Unknown file type: {file_type}"
|
| 218 |
+
|
| 219 |
+
return info + result
|
| 220 |
+
|
| 221 |
+
except Exception as e:
|
| 222 |
+
return info + f"Error processing {file_type}: {str(e)}"
|
| 223 |
+
|
| 224 |
if __name__ == "__main__":
|
| 225 |
print("Starting FastMCP server on port 7860...")
|
| 226 |
|