Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
import os
|
| 2 |
-
os.environ["GRADIO_SSR_MODE"] = "false"
|
| 3 |
|
| 4 |
import torch
|
| 5 |
import spaces
|
| 6 |
import gradio as gr
|
| 7 |
from transformers import AutoModelForImageTextToText, AutoProcessor
|
| 8 |
from huggingface_hub import login
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Login with HF token if available
|
| 11 |
hf_token = os.environ.get("HF_TOKEN")
|
|
@@ -91,103 +93,7 @@ def translate(text: str, source_lang: str, target_lang: str) -> str:
|
|
| 91 |
return output.strip()
|
| 92 |
|
| 93 |
|
| 94 |
-
from fastapi import FastAPI, Request
|
| 95 |
-
from fastapi.responses import JSONResponse
|
| 96 |
-
|
| 97 |
-
app = FastAPI()
|
| 98 |
-
|
| 99 |
-
@app.get("/health")
|
| 100 |
-
async def health_check():
|
| 101 |
-
return {"status": "ok"}
|
| 102 |
-
|
| 103 |
LANG_CODES = list(LANGUAGES.keys())
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
@app.post("/mcp")
|
| 107 |
-
async def mcp_handler(request: Request):
|
| 108 |
-
body = await request.json()
|
| 109 |
-
method = body.get("method", "")
|
| 110 |
-
params = body.get("params", {})
|
| 111 |
-
msg_id = body.get("id")
|
| 112 |
-
|
| 113 |
-
if method == "initialize":
|
| 114 |
-
return JSONResponse({
|
| 115 |
-
"jsonrpc": "2.0",
|
| 116 |
-
"id": msg_id,
|
| 117 |
-
"result": {
|
| 118 |
-
"protocolVersion": "2024-11-05",
|
| 119 |
-
"capabilities": {"tools": {}},
|
| 120 |
-
"serverInfo": {
|
| 121 |
-
"name": "translategemma-mcp",
|
| 122 |
-
"version": "1.0.0"
|
| 123 |
-
}
|
| 124 |
-
}
|
| 125 |
-
})
|
| 126 |
-
|
| 127 |
-
elif method == "tools/list":
|
| 128 |
-
return JSONResponse({
|
| 129 |
-
"jsonrpc": "2.0",
|
| 130 |
-
"id": msg_id,
|
| 131 |
-
"result": {
|
| 132 |
-
"tools": [
|
| 133 |
-
{
|
| 134 |
-
"name": "translate",
|
| 135 |
-
"description": "Translate text between 55 languages using TranslateGemma-4B-IT",
|
| 136 |
-
"inputSchema": {
|
| 137 |
-
"type": "object",
|
| 138 |
-
"properties": {
|
| 139 |
-
"text": {
|
| 140 |
-
"type": "string",
|
| 141 |
-
"description": "The text to translate"
|
| 142 |
-
},
|
| 143 |
-
"source_lang": {
|
| 144 |
-
"type": "string",
|
| 145 |
-
"description": f"Source language code: {', '.join(LANG_CODES)}"
|
| 146 |
-
},
|
| 147 |
-
"target_lang": {
|
| 148 |
-
"type": "string",
|
| 149 |
-
"description": f"Target language code: {', '.join(LANG_CODES)}"
|
| 150 |
-
}
|
| 151 |
-
},
|
| 152 |
-
"required": ["text", "source_lang", "target_lang"]
|
| 153 |
-
}
|
| 154 |
-
}
|
| 155 |
-
]
|
| 156 |
-
}
|
| 157 |
-
})
|
| 158 |
-
|
| 159 |
-
elif method == "tools/call":
|
| 160 |
-
tool_name = params.get("name")
|
| 161 |
-
arguments = params.get("arguments", {})
|
| 162 |
-
|
| 163 |
-
if tool_name == "translate":
|
| 164 |
-
try:
|
| 165 |
-
result = translate(
|
| 166 |
-
arguments.get("text", ""),
|
| 167 |
-
arguments.get("source_lang", "en"),
|
| 168 |
-
arguments.get("target_lang", "en")
|
| 169 |
-
)
|
| 170 |
-
return JSONResponse({
|
| 171 |
-
"jsonrpc": "2.0",
|
| 172 |
-
"id": msg_id,
|
| 173 |
-
"result": {
|
| 174 |
-
"content": [{"type": "text", "text": result}]
|
| 175 |
-
}
|
| 176 |
-
})
|
| 177 |
-
except Exception as e:
|
| 178 |
-
return JSONResponse({
|
| 179 |
-
"jsonrpc": "2.0",
|
| 180 |
-
"id": msg_id,
|
| 181 |
-
"error": {"code": -32000, "message": str(e)}
|
| 182 |
-
})
|
| 183 |
-
|
| 184 |
-
return JSONResponse({
|
| 185 |
-
"jsonrpc": "2.0",
|
| 186 |
-
"id": msg_id,
|
| 187 |
-
"error": {"code": -32601, "message": f"Method not found: {method}"}
|
| 188 |
-
})
|
| 189 |
-
|
| 190 |
-
|
| 191 |
LANG_CHOICES = [f"{code} ({name})" for code, name in LANGUAGES.items()]
|
| 192 |
|
| 193 |
|
|
@@ -197,6 +103,7 @@ def gradio_translate(text, source, target):
|
|
| 197 |
return translate(text, src_code, tgt_code)
|
| 198 |
|
| 199 |
|
|
|
|
| 200 |
with gr.Blocks(title="TranslateGemma") as demo:
|
| 201 |
|
| 202 |
gr.HTML("""
|
|
@@ -385,8 +292,90 @@ with gr.Blocks(title="TranslateGemma") as demo:
|
|
| 385 |
outputs=output_text
|
| 386 |
)
|
| 387 |
|
| 388 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
| 389 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
if __name__ == "__main__":
|
| 391 |
-
|
| 392 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.environ["GRADIO_SSR_MODE"] = "false"
|
| 3 |
|
| 4 |
import torch
|
| 5 |
import spaces
|
| 6 |
import gradio as gr
|
| 7 |
from transformers import AutoModelForImageTextToText, AutoProcessor
|
| 8 |
from huggingface_hub import login
|
| 9 |
+
from fastapi import Request
|
| 10 |
+
from fastapi.responses import JSONResponse
|
| 11 |
|
| 12 |
# Login with HF token if available
|
| 13 |
hf_token = os.environ.get("HF_TOKEN")
|
|
|
|
| 93 |
return output.strip()
|
| 94 |
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
LANG_CODES = list(LANGUAGES.keys())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
LANG_CHOICES = [f"{code} ({name})" for code, name in LANGUAGES.items()]
|
| 98 |
|
| 99 |
|
|
|
|
| 103 |
return translate(text, src_code, tgt_code)
|
| 104 |
|
| 105 |
|
| 106 |
+
# ✅ Gradio UI 정의
|
| 107 |
with gr.Blocks(title="TranslateGemma") as demo:
|
| 108 |
|
| 109 |
gr.HTML("""
|
|
|
|
| 292 |
outputs=output_text
|
| 293 |
)
|
| 294 |
|
|
|
|
| 295 |
|
| 296 |
+
# ✅ Gradio 앱 실행 후 FastAPI 라우트 추가
|
| 297 |
+
app = demo.app # Gradio 내부 FastAPI 앱 가져오기
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
@app.get("/health")
|
| 301 |
+
async def health_check():
|
| 302 |
+
return {"status": "ok"}
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
@app.post("/mcp")
|
| 306 |
+
async def mcp_handler(request: Request):
|
| 307 |
+
body = await request.json()
|
| 308 |
+
method = body.get("method", "")
|
| 309 |
+
params = body.get("params", {})
|
| 310 |
+
msg_id = body.get("id")
|
| 311 |
+
|
| 312 |
+
if method == "initialize":
|
| 313 |
+
return JSONResponse({
|
| 314 |
+
"jsonrpc": "2.0",
|
| 315 |
+
"id": msg_id,
|
| 316 |
+
"result": {
|
| 317 |
+
"protocolVersion": "2024-11-05",
|
| 318 |
+
"capabilities": {"tools": {}},
|
| 319 |
+
"serverInfo": {
|
| 320 |
+
"name": "translategemma-mcp",
|
| 321 |
+
"version": "1.0.0"
|
| 322 |
+
}
|
| 323 |
+
}
|
| 324 |
+
})
|
| 325 |
+
|
| 326 |
+
elif method == "tools/list":
|
| 327 |
+
return JSONResponse({
|
| 328 |
+
"jsonrpc": "2.0",
|
| 329 |
+
"id": msg_id,
|
| 330 |
+
"result": {
|
| 331 |
+
"tools": [
|
| 332 |
+
{
|
| 333 |
+
"name": "translate",
|
| 334 |
+
"description": "Translate text between 55 languages using TranslateGemma-4B-IT",
|
| 335 |
+
"inputSchema": {
|
| 336 |
+
"type": "object",
|
| 337 |
+
"properties": {
|
| 338 |
+
"text": {"type": "string", "description": "The text to translate"},
|
| 339 |
+
"source_lang": {"type": "string", "description": f"Source language code: {', '.join(LANG_CODES)}"},
|
| 340 |
+
"target_lang": {"type": "string", "description": f"Target language code: {', '.join(LANG_CODES)}"}
|
| 341 |
+
},
|
| 342 |
+
"required": ["text", "source_lang", "target_lang"]
|
| 343 |
+
}
|
| 344 |
+
}
|
| 345 |
+
]
|
| 346 |
+
}
|
| 347 |
+
})
|
| 348 |
+
|
| 349 |
+
elif method == "tools/call":
|
| 350 |
+
tool_name = params.get("name")
|
| 351 |
+
arguments = params.get("arguments", {})
|
| 352 |
+
|
| 353 |
+
if tool_name == "translate":
|
| 354 |
+
try:
|
| 355 |
+
result = translate(
|
| 356 |
+
arguments.get("text", ""),
|
| 357 |
+
arguments.get("source_lang", "en"),
|
| 358 |
+
arguments.get("target_lang", "en")
|
| 359 |
+
)
|
| 360 |
+
return JSONResponse({
|
| 361 |
+
"jsonrpc": "2.0",
|
| 362 |
+
"id": msg_id,
|
| 363 |
+
"result": {"content": [{"type": "text", "text": result}]}
|
| 364 |
+
})
|
| 365 |
+
except Exception as e:
|
| 366 |
+
return JSONResponse({
|
| 367 |
+
"jsonrpc": "2.0",
|
| 368 |
+
"id": msg_id,
|
| 369 |
+
"error": {"code": -32000, "message": str(e)}
|
| 370 |
+
})
|
| 371 |
+
|
| 372 |
+
return JSONResponse({
|
| 373 |
+
"jsonrpc": "2.0",
|
| 374 |
+
"id": msg_id,
|
| 375 |
+
"error": {"code": -32601, "message": f"Method not found: {method}"}
|
| 376 |
+
})
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
# ✅ Gradio launch로 실행 (FastAPI mount 대신)
|
| 380 |
if __name__ == "__main__":
|
| 381 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|