Text_emotion / main.py
tensorgirl's picture
Update main.py
ec9a090 verified
from fastapi import FastAPI
import os
import json
import google.generativeai as genai
from pydantic import BaseModel, validator
class Item(BaseModel):
text: str = "sddddddddddd"
app = FastAPI()
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
genai.configure(api_key=GOOGLE_API_KEY)
# Set up the model
generation_config = {
"temperature": 0.9,
"top_p": 1,
"top_k": 1,
"max_output_tokens": 2048,
}
model = genai.GenerativeModel(
model_name="gemini-pro",
generation_config=generation_config,
)
task_description = " You need to classify each message you receive among the following categories: 'admiration','amusement','anger','annoyance','approval','caring','confusion','curiosity','desire','disappointment','disapproval','disgust','embarrassment','excitement','fear','gratitude','grief','joy','love','nervousness', 'optimism', 'pride', 'realization', 'relief', 'remorse', 'sadness', 'surprise', 'neutral'<div>The output must be in JSON format</div>"
def classify_msg(message):
prompt_parts = [
task_description,
f"Message: {message['text']}",
"Category: ",
]
response = model.generate_content(prompt_parts)
json_response = json.loads(
response.text[response.text.find("{") : response.text.rfind("}") + 1]
)
return json_response['category']
@app.get("/")
async def root():
return {"Text Emotion Classification":"Version 1.5 'Text'"}
@app.post("/classify/")
def read_user(js: Item):
return classify_msg(js.dict())