gigswar commited on
Commit
a2f9224
·
verified ·
1 Parent(s): 9cd738b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -43
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
- # Initialize FastAPI app
12
- app = FastAPI()
13
-
14
- # CORS for browser requests
15
- app.add_middleware(
16
- CORSMiddleware,
17
- allow_origins=["*"],
18
- allow_credentials=True,
19
- allow_methods=["*"],
20
- allow_headers=["*"],
 
 
 
 
 
 
 
 
 
21
  )
22
 
23
- # Route to handle GPT-4o Interview Q&A
24
- @app.post("/ask")
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()