Update main.py
Browse files
main.py
CHANGED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from groq import Groq
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
# Load environment variables from a .env file
|
| 8 |
+
load_dotenv() # reads .env in project root by default
|
| 9 |
+
|
| 10 |
+
API_KEY = os.getenv("GROQ_API_KEY")
|
| 11 |
+
if not API_KEY:
|
| 12 |
+
raise RuntimeError("Please set GROQ_API_KEY in your .env file")
|
| 13 |
+
|
| 14 |
+
# Initialize Groq client once
|
| 15 |
+
client = Groq(api_key=API_KEY)
|
| 16 |
+
|
| 17 |
+
def translate_text(
|
| 18 |
+
text: str,
|
| 19 |
+
target_language: str,
|
| 20 |
+
model: str = "llama-3.3-70b-versatile",
|
| 21 |
+
temperature: float = 0.7,
|
| 22 |
+
stream: bool = False
|
| 23 |
+
) -> str:
|
| 24 |
+
"""
|
| 25 |
+
Sends a translation prompt to the LLM and returns the translated text.
|
| 26 |
+
"""
|
| 27 |
+
messages = [
|
| 28 |
+
{"role": "system", "content": "You are a helpful, accurate translator."},
|
| 29 |
+
{
|
| 30 |
+
"role": "user",
|
| 31 |
+
"content": (
|
| 32 |
+
f"Translate the following text into {target_language}:\n\n"
|
| 33 |
+
f"{text}"
|
| 34 |
+
)
|
| 35 |
+
}
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
completion = client.chat.completions.create(
|
| 39 |
+
model=model,
|
| 40 |
+
messages=messages,
|
| 41 |
+
temperature=temperature,
|
| 42 |
+
max_completion_tokens=1024,
|
| 43 |
+
top_p=1,
|
| 44 |
+
stream=stream,
|
| 45 |
+
stop=None,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
if not stream:
|
| 49 |
+
return completion.choices[0].message.content.strip()
|
| 50 |
+
else:
|
| 51 |
+
result = []
|
| 52 |
+
for chunk in completion:
|
| 53 |
+
delta = chunk.choices[0].delta.content
|
| 54 |
+
if delta:
|
| 55 |
+
result.append(delta)
|
| 56 |
+
return "".join(result).strip()
|
| 57 |
+
|
| 58 |
+
# FastAPI setup
|
| 59 |
+
app = FastAPI(title="Groq Translator API")
|
| 60 |
+
|
| 61 |
+
class TranslateRequest(BaseModel):
|
| 62 |
+
text: str
|
| 63 |
+
target_language: str
|
| 64 |
+
stream: bool = False
|
| 65 |
+
|
| 66 |
+
class TranslateResponse(BaseModel):
|
| 67 |
+
translation: str
|
| 68 |
+
|
| 69 |
+
@app.post("/translate", response_model=TranslateResponse)
|
| 70 |
+
async def translate_endpoint(req: TranslateRequest):
|
| 71 |
+
"""
|
| 72 |
+
POST /translate
|
| 73 |
+
{
|
| 74 |
+
"text": "Hello, world!",
|
| 75 |
+
"target_language": "French",
|
| 76 |
+
"stream": false
|
| 77 |
+
}
|
| 78 |
+
=>
|
| 79 |
+
{
|
| 80 |
+
"translation": "Bonjour, le monde !"
|
| 81 |
+
}
|
| 82 |
+
"""
|
| 83 |
+
try:
|
| 84 |
+
translated = translate_text(
|
| 85 |
+
text=req.text,
|
| 86 |
+
target_language=req.target_language,
|
| 87 |
+
stream=req.stream
|
| 88 |
+
)
|
| 89 |
+
return TranslateResponse(translation=translated)
|
| 90 |
+
except Exception as e:
|
| 91 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 92 |
+
|
| 93 |
+
# To run:
|
| 94 |
+
# 1. pip install fastapi uvicorn groq python-dotenv
|
| 95 |
+
# 2. create a .env file in the project root with:
|
| 96 |
+
# GROQ_API_KEY=your_real_api_key_here
|
| 97 |
+
# 3. uvicorn main:app --reload --port 7860
|