aimanathar commited on
Commit
429d451
·
verified ·
1 Parent(s): 431d9f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -19
app.py CHANGED
@@ -1,25 +1,68 @@
1
- import gradio as gr
 
2
  import requests
 
 
 
3
 
4
- API_URL = "https://your-fastapi-url.onrender.com/auto_feedback" # change to your FastAPI deployment URL
 
5
 
6
- def get_feedback(user_input):
7
- payload = {"message": user_input}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  try:
9
- res = requests.post(API_URL, json=payload)
10
- data = res.json()
11
- ai_msg = data.get("ai_response", "No response received.")
12
- pulse_status = data.get("pulse_status", "N/A")
13
- return f"💬 AI Feedback: {ai_msg}\n📡 Pulse API Status: {pulse_status}"
14
- except Exception as e:
15
- return f"⚠️ Error: {e}"
16
 
17
- iface = gr.Interface(
18
- fn=get_feedback,
19
- inputs=gr.Textbox(label="Ask about your survey"),
20
- outputs="text",
21
- title="Feedback Chatbot",
22
- description="Get AI-powered feedback and send it to Pulse Survey API automatically.",
23
- )
 
 
24
 
25
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ import os
3
  import requests
4
+ from pydantic import BaseModel
5
+ from dotenv import load_dotenv
6
+ import openai
7
 
8
+ # Load Hugging Face injected secrets
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: Generate AI feedback + recommendation
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-2 sentences)
37
+ 2. A practical recommendation for improvement.
38
+ Return as JSON with keys: 'feedback' and 'recommendation'.
39
+ """
40
 
41
+ completion = openai.ChatCompletion.create(
42
+ model="gpt-3.5-turbo",
43
+ messages=[{"role": "system", "content": ai_prompt}]
44
+ )
45
+
46
+ ai_text = completion.choices[0].message["content"]
47
+
48
+ # Step 2: Send to Pulse Survey API
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: Return structured result to chatbot
60
+ return {
61
+ "status": "success",
62
+ "user_input": user_input,
63
+ "ai_response": ai_text,
64
+ "pulse_status": pulse_response.status_code,
65
+ }
66
+
67
+ except Exception as e:
68
+ return {"status": "error", "message": str(e)}