Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, HTTPException | |
from pydantic import BaseModel | |
from openai import OpenAI | |
# Initialiser le client OpenAI | |
client = OpenAI( | |
base_url="https://integrate.api.nvidia.com/v1", | |
api_key="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" | |
) | |
# Définir les prompts | |
DEFAULT_PROMPT2 = """You are Kittycara, a friendly AI assistant designed to help adolescent girls and their caretakers understand menstrual health. | |
Your goal is to provide support, information, and potential diagnoses based on the symptoms provided. Remember to be sensitive, supportive, and | |
encourage seeking professional medical advice when necessary. Always maintain a friendly and approachable tone, as if you were a caring pet cat. | |
Always explain medical terms in a way that is easy to understand. For example, if you mention "menstruation," explain it as 'the monthly bleeding women experience as part of their reproductive cycle.' | |
If asked about topics outside of menstrual health or medical information, politely state that you're not able to discuss those subjects | |
and redirect the conversation to menstrual health concerns. Always encourage seeking professional medical advice for specific diagnoses or treatments.""" | |
SYMPTOMS = [ | |
"Heavy bleeding", "Irregular periods", "Painful periods", "Missed periods", | |
"Spotting between periods", "Mood swings", "Fatigue", "Abdominal pain", | |
"Nausea", "Headaches", "Breast tenderness", "Acne" | |
] | |
# Définir les classes de données d'entrée avec Pydantic | |
class RequestData(BaseModel): | |
name: str | |
age: int | |
sex: str | |
message: str | |
history: list | |
symptoms: list | |
# Initialiser l'application FastAPI | |
app = FastAPI() | |
# Fonction pour obtenir un message personnalisé basé sur les symptômes | |
def get_reassurance_message(symptoms): | |
if "Painful periods" in symptoms or "Abdominal pain" in symptoms: | |
return "I know that pain can be tough, but you're doing great, and it's important to listen to your body. 🫂 Take care, and don't hesitate to reach out to a healthcare professional!" | |
elif "Mood swings" in symptoms or "Fatigue" in symptoms: | |
return "It's completely normal to feel this way sometimes. Don't worry, you're not alone, and things will get better. 🌼 Stay strong!" | |
elif "Heavy bleeding" in symptoms: | |
return "Heavy bleeding can be concerning, but there are options to help manage it. 🧡 It’s always good to talk to a doctor to make sure everything's okay." | |
else: | |
return "You're doing great by paying attention to your health. 💪 Keep going, and don't hesitate to ask for help if you need it!" | |
# Fonction pour prédire la réponse | |
def predict(name, age, sex, message, history, symptoms): | |
messages = [{"role": "system", "content": DEFAULT_PROMPT2}] | |
for human, assistant in history: | |
messages.append({"role": "user", "content": human}) | |
messages.append({"role": "assistant", "content": assistant}) | |
selected_symptoms = ", ".join([sym for sym in symptoms if sym]) | |
full_message = f"Name: {name}, Age: {age}, Sex: {sex}\nSymptoms: {selected_symptoms}\nAdditional information: {message}" | |
messages.append({"role": "user", "content": full_message}) | |
try: | |
completion = client.chat.completions.create( | |
model="meta/llama-3.1-8b-instruct", | |
messages=messages, | |
temperature=0.2, | |
top_p=0.9, | |
max_tokens=1024, | |
stream=True | |
) | |
full_response = "" | |
for chunk in completion: | |
if chunk.choices[0].delta.content is not None: | |
full_response += chunk.choices[0].delta.content | |
return full_response | |
except Exception as e: | |
return f"Erreur : {str(e)}" | |
# Point de départ pour lancer l'application FastAPI avec Uvicorn | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="0.0.0.0", port=8000) | |