Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,35 +5,39 @@ from pydantic import BaseModel
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
import openai
|
| 7 |
|
| 8 |
-
# ✅ Load Hugging Face
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
-
# FastAPI app
|
| 12 |
-
app = FastAPI()
|
| 13 |
|
| 14 |
-
# Read secrets from environment
|
| 15 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 16 |
PULSE_API_URL = os.getenv("PULSE_API_URL")
|
| 17 |
PULSE_API_KEY = os.getenv("PULSE_API_KEY")
|
| 18 |
|
| 19 |
-
# Configure OpenAI
|
| 20 |
openai.api_key = OPENAI_API_KEY
|
| 21 |
|
| 22 |
-
# Pydantic model for chatbot message
|
| 23 |
class Message(BaseModel):
|
| 24 |
text: str
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
@app.post("/auto_feedback")
|
| 27 |
async def auto_feedback(msg: Message):
|
| 28 |
try:
|
| 29 |
user_input = msg.text
|
| 30 |
|
| 31 |
-
# Step 1
|
| 32 |
ai_prompt = f"""
|
| 33 |
-
You are an HR feedback assistant.
|
| 34 |
A user said: "{user_input}"
|
| 35 |
Generate:
|
| 36 |
-
1. A short professional feedback (1
|
| 37 |
2. A practical recommendation for improvement.
|
| 38 |
Return as JSON with keys: 'feedback' and 'recommendation'.
|
| 39 |
"""
|
|
@@ -45,18 +49,15 @@ async def auto_feedback(msg: Message):
|
|
| 45 |
|
| 46 |
ai_text = completion.choices[0].message["content"]
|
| 47 |
|
| 48 |
-
# Step 2
|
| 49 |
pulse_response = requests.post(
|
| 50 |
f"{PULSE_API_URL}/pulse-survey-answers/store",
|
| 51 |
headers={"Authorization": f"Bearer {PULSE_API_KEY}"},
|
| 52 |
-
json={
|
| 53 |
-
"question": user_input,
|
| 54 |
-
"answer": ai_text,
|
| 55 |
-
},
|
| 56 |
timeout=10
|
| 57 |
)
|
| 58 |
|
| 59 |
-
# Step 3
|
| 60 |
return {
|
| 61 |
"status": "success",
|
| 62 |
"user_input": user_input,
|
|
@@ -66,3 +67,8 @@ async def auto_feedback(msg: Message):
|
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
return {"status": "error", "message": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
import openai
|
| 7 |
|
| 8 |
+
# ✅ Load environment variables (from Hugging Face secrets)
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
+
# ✅ Initialize FastAPI app
|
| 12 |
+
app = FastAPI(title="AI Feedback Engine")
|
| 13 |
|
| 14 |
+
# ✅ Read secrets from environment variables
|
| 15 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 16 |
PULSE_API_URL = os.getenv("PULSE_API_URL")
|
| 17 |
PULSE_API_KEY = os.getenv("PULSE_API_KEY")
|
| 18 |
|
| 19 |
+
# ✅ Configure OpenAI
|
| 20 |
openai.api_key = OPENAI_API_KEY
|
| 21 |
|
| 22 |
+
# ✅ Pydantic model for chatbot message
|
| 23 |
class Message(BaseModel):
|
| 24 |
text: str
|
| 25 |
|
| 26 |
+
@app.get("/")
|
| 27 |
+
def home():
|
| 28 |
+
return {"message": "🚀 AI Feedback Engine is running!"}
|
| 29 |
+
|
| 30 |
@app.post("/auto_feedback")
|
| 31 |
async def auto_feedback(msg: Message):
|
| 32 |
try:
|
| 33 |
user_input = msg.text
|
| 34 |
|
| 35 |
+
# Step 1️⃣: Generate AI feedback + recommendation
|
| 36 |
ai_prompt = f"""
|
| 37 |
+
You are an HR feedback assistant.
|
| 38 |
A user said: "{user_input}"
|
| 39 |
Generate:
|
| 40 |
+
1. A short, professional feedback (1–2 sentences)
|
| 41 |
2. A practical recommendation for improvement.
|
| 42 |
Return as JSON with keys: 'feedback' and 'recommendation'.
|
| 43 |
"""
|
|
|
|
| 49 |
|
| 50 |
ai_text = completion.choices[0].message["content"]
|
| 51 |
|
| 52 |
+
# Step 2️⃣: Send to Pulse Survey API
|
| 53 |
pulse_response = requests.post(
|
| 54 |
f"{PULSE_API_URL}/pulse-survey-answers/store",
|
| 55 |
headers={"Authorization": f"Bearer {PULSE_API_KEY}"},
|
| 56 |
+
json={"question": user_input, "answer": ai_text},
|
|
|
|
|
|
|
|
|
|
| 57 |
timeout=10
|
| 58 |
)
|
| 59 |
|
| 60 |
+
# Step 3️⃣: Return structured result to chatbot
|
| 61 |
return {
|
| 62 |
"status": "success",
|
| 63 |
"user_input": user_input,
|
|
|
|
| 67 |
|
| 68 |
except Exception as e:
|
| 69 |
return {"status": "error", "message": str(e)}
|
| 70 |
+
|
| 71 |
+
# ✅ This part ensures it runs locally too (optional)
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
import uvicorn
|
| 74 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|