-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
48 lines (36 loc) · 1.36 KB
/
main.py
File metadata and controls
48 lines (36 loc) · 1.36 KB
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
import os
from dotenv import load_dotenv
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import ChatPromptTemplate
load_dotenv()
app = FastAPI(
title="Basic Translation API",
description="A simple API to translate text using LangChain and Gemini."
)
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
class TranslationRequest(BaseModel):
text: str
target_language: str
prompt = ChatPromptTemplate.from_template(
"Translate the following text into {target_language}. "
"Only return the final translated text and nothing else.\n"
"Text: {text}"
)
model = ChatGoogleGenerativeAI(model="gemini-1.5-flash", temperature=0)
chain = prompt | model
@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.post("/translate")
async def translate_text(request: TranslationRequest):
result = chain.invoke({
"text": request.text,
"target_language": request.target_language
})
return {"translated_text": result.content}