File size: 2,335 Bytes
c1dac64
9f0a178
 
f7cc9ad
8b05c02
d120873
e46dbee
 
f7cc9ad
 
 
d120873
c1dac64
4e5cd58
 
eaec419
4e5cd58
 
 
eaec419
4e5cd58
 
eaec419
4e5cd58
eaec419
 
 
4e5cd58
 
 
 
 
 
 
 
 
d120873
 
184e6ec
698fb3f
fd2637b
d120873
 
184e6ec
5936387
786aa14
4e95015
 
 
8b05c02
 
 
 
27ced65
c832cd2
8b05c02
 
4e95015
 
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
from fastapi import FastAPI, Request
import src.Paraphrase as Paraphrase
import src.Translate as Translate
from typing import Optional
from fastapi_mcp import FastApiMCP

app = FastAPI()
# app = FastAPI(docs_url="/docs")
MTMODELS = {'enro': 'BlackKakapo/opus-mt-en-ro',
            'roen': 'BlackKakapo/opus-mt-ro-en',
            'gemma': 'Gargaz/gemma-2b-romanian-better'}

@app.get("/")
def index(request: Request):
    from fastapi.responses import HTMLResponse
    host_url = "https://" + request.url.netloc
    html_content = f'''
        <html>
            <head>
                <title>FastAPI with MCP</title>
            </head>
            <body>
                <h1>FastAPI URLS</h1>
                <p>Host URL: {host_url}</p>
                <p><a href="{host_url}/docs">DOCS</a></p>
                <p><a href="{host_url}/redoc">REDOC</a></p>
                <p><a href="{host_url}/openapi.json">openapi.json</a></p>
            </body>
        </html>
        '''
    return HTMLResponse(content=html_content)

# @app.get("/")
# async def get_host_url(request: Request):
#     host_url = request.url.scheme + "://" + request.url.netloc
#     return {"host_url": host_url, 'endpoints': ['/paraphrase', '/translate', f'{host_url}/docs', f'{host_url}/redoc', f'{host_url}/openapi.json'], 'mtmodels': MTMODELS}

@app.get("/paraphrase")
def paraphrase(text: str, model: str, operation_id="get_paraphrase", description="Paraphrase text"):
    resultValue, exception = Paraphrase.paraphraseParaphraseMethod(text, model)
    return {"input": text, "translation": resultValue, "exception": exception}

@app.get("/translate")
def translate(text: str, model: Optional[str] = MTMODELS['gemma'], operation_id="get_translate", description="Translate text"):
    # resultValue, exception = Translate.paraphraseTranslateMethod(text, model)
    resultValue: str = Translate.gemma_direct(text, model)
    return {"input": text, "result": resultValue, "model": model}

# Create an MCP server based on this app
mcp = FastApiMCP(
    app,
    name="FASTAPI translate and paraphrase MCP",
    description="MCP server to translate and paraphrase text",
    describe_all_responses=True,
    describe_full_response_schema=True,
    include_operations=["get_translate", "get_paraphrase"]
)
# Mount the MCP server directly to your app
mcp.mount()