Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,24 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
connections = {}
|
| 8 |
|
| 9 |
@app.websocket("/ws/{client_id}")
|
|
@@ -12,12 +27,28 @@ async def websocket_endpoint(websocket: WebSocket, client_id: str):
|
|
| 12 |
connections[client_id] = websocket
|
| 13 |
try:
|
| 14 |
while True:
|
| 15 |
-
|
| 16 |
-
message
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
from fastapi import FastAPI, WebSocket
|
| 4 |
+
from fastapi.responses import HTMLResponse
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
openai.api_key = os.getenv("OPENAI_API_KEY") # Use an environment variable
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
# Serve the index.html page via Gradio
|
| 12 |
+
def serve_ui():
|
| 13 |
+
with open("index.html", "r") as f:
|
| 14 |
+
return f.read()
|
| 15 |
+
|
| 16 |
+
gradio_app = gr.Interface(fn=serve_ui, inputs=[], outputs="html")
|
| 17 |
+
|
| 18 |
+
# Mount Gradio inside FastAPI
|
| 19 |
+
app.mount("/", gradio_app)
|
| 20 |
+
|
| 21 |
+
# WebSocket signaling for WebRTC (basic forwarder)
|
| 22 |
connections = {}
|
| 23 |
|
| 24 |
@app.websocket("/ws/{client_id}")
|
|
|
|
| 27 |
connections[client_id] = websocket
|
| 28 |
try:
|
| 29 |
while True:
|
| 30 |
+
data = await websocket.receive_text()
|
| 31 |
+
# Forward message to all other clients
|
| 32 |
+
for cid, conn in connections.items():
|
| 33 |
+
if cid != client_id:
|
| 34 |
+
await conn.send_text(data)
|
| 35 |
+
except:
|
| 36 |
+
connections.pop(client_id, None)
|
| 37 |
+
|
| 38 |
+
# GPT-4o-based Interview Q&A endpoint
|
| 39 |
+
@app.post("/ask")
|
| 40 |
+
async def ask_question(payload: dict):
|
| 41 |
+
user_input = payload.get("message", "")
|
| 42 |
+
if not user_input:
|
| 43 |
+
return {"response": "No question provided."}
|
| 44 |
+
|
| 45 |
+
response = openai.ChatCompletion.create(
|
| 46 |
+
model="gpt-4o",
|
| 47 |
+
messages=[
|
| 48 |
+
{"role": "system", "content": "You are an AI interviewer. Ask one question at a time."},
|
| 49 |
+
{"role": "user", "content": user_input}
|
| 50 |
+
]
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
reply = response.choices[0].message["content"]
|
| 54 |
+
return {"response": reply}
|