Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,29 @@
|
|
| 1 |
import os
|
| 2 |
import openai
|
| 3 |
import gradio as gr
|
| 4 |
-
from fastapi import FastAPI, Request
|
| 5 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
-
from gradio.routes import mount_gradio_app
|
| 7 |
|
| 8 |
-
# Load OpenAI API Key from Hugging Face Secrets
|
| 9 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
async def ask_question(request: Request):
|
| 26 |
-
payload = await request.json()
|
| 27 |
-
user_input = payload.get("message", "")
|
| 28 |
-
|
| 29 |
-
if not user_input:
|
| 30 |
-
return {"response": "Please say something."}
|
| 31 |
-
|
| 32 |
-
try:
|
| 33 |
-
response = openai.ChatCompletion.create(
|
| 34 |
-
model="gpt-4o",
|
| 35 |
-
messages=[
|
| 36 |
-
{"role": "system", "content": "You are a smart, friendly AI interviewer. Ask one question at a time."},
|
| 37 |
-
{"role": "user", "content": user_input}
|
| 38 |
-
]
|
| 39 |
-
)
|
| 40 |
-
reply = response.choices[0].message["content"]
|
| 41 |
-
return {"response": reply}
|
| 42 |
-
except Exception as e:
|
| 43 |
-
return {"response": f"Error: {str(e)}"}
|
| 44 |
-
|
| 45 |
-
# Serve the frontend HTML using Gradio (just wraps index.html)
|
| 46 |
-
def render_ui():
|
| 47 |
-
with open("index.html", "r") as f:
|
| 48 |
-
return f.read()
|
| 49 |
-
|
| 50 |
-
gradio_ui = gr.Interface(fn=render_ui, inputs=[], outputs="html")
|
| 51 |
-
app = mount_gradio_app(app, gradio_ui, path="/")
|
|
|
|
| 1 |
import os
|
| 2 |
import openai
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
| 5 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 6 |
|
| 7 |
+
def chat_with_gpt4o(message):
|
| 8 |
+
if not message:
|
| 9 |
+
return "Please say something."
|
| 10 |
+
|
| 11 |
+
response = openai.ChatCompletion.create(
|
| 12 |
+
model="gpt-4o",
|
| 13 |
+
messages=[
|
| 14 |
+
{"role": "system", "content": "You are a smart, friendly AI interviewer. Ask one question at a time."},
|
| 15 |
+
{"role": "user", "content": message}
|
| 16 |
+
]
|
| 17 |
+
)
|
| 18 |
+
return response.choices[0].message["content"]
|
| 19 |
+
|
| 20 |
+
iface = gr.Interface(
|
| 21 |
+
fn=chat_with_gpt4o,
|
| 22 |
+
inputs=gr.Textbox(placeholder="Say something..."),
|
| 23 |
+
outputs=gr.Textbox(label="AI Interviewer Response"),
|
| 24 |
+
title="🎤 AI Interview Bot",
|
| 25 |
+
description="Talk to a GPT-4o powered AI Interviewer. One-on-one style."
|
| 26 |
)
|
| 27 |
|
| 28 |
+
if __name__ == "__main__":
|
| 29 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|