Spaces:
Sleeping
Sleeping
File size: 1,955 Bytes
d5d3869 0a8a0f3 d5d3869 0a8a0f3 d5d3869 0a8a0f3 d5d3869 0a8a0f3 d5d3869 0a8a0f3 d5d3869 0a8a0f3 d5d3869 0a8a0f3 610b8d4 |
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 |
import os
import google.generativeai as genai
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI()
# Получаем ключ
genai.configure(api_key=os.environ.get("GEMINI_API_KEY"))
# ВАЖНО: Убедись, что в requirements.txt версия >=0.7.2, иначе flash не заработает
model = genai.GenerativeModel("gemini-1.5-flash")
class RequestData(BaseModel):
context: str
mood: str
@app.post("/complete")
async def complete_text(data: RequestData):
try:
# Настройка личности
tone_instruction = ""
if data.mood == "fun":
tone_instruction = "Отвечай весело, используй сленг, эмодзи."
elif data.mood == "serious":
tone_instruction = "Отвечай официально, сухо, деловой стиль."
elif data.mood == "angry":
tone_instruction = "Отвечай пассивно-агрессивно, с сарказмом."
else:
tone_instruction = "Отвечай спокойно и нейтрально."
# ТВОЙ НОВЫЙ ПРОМПТ
prompt = (
f"System: {tone_instruction}. "
f"User input might be in wrong keyboard layout (e.g. 'ghbdtn' instead of 'привет'). "
f"If input looks like encoded Russian, decode it internally and complete the Russian phrase. "
f"If input is English, just complete it. "
f"Do not repeat the input. Output ONLY the completion text.\n\n"
f"User Input: {data.context}\nOutput:"
)
response = model.generate_content(prompt)
return {"completion": response.text.strip()}
except Exception as e:
# Вывод ошибки в ответ, чтобы клиент увидел причину
raise HTTPException(status_code=500, detail=str(e)) |