taldemir commited on
Commit
7afcaab
·
verified ·
1 Parent(s): c92f9f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -34
app.py CHANGED
@@ -20,11 +20,11 @@ client = OpenAI(api_key=OPENAI_API_KEY)
20
  fastapi_app = FastAPI()
21
 
22
  @fastapi_app.get("/initial_prompt")
23
- def get_initial_prompt():
24
  return {"prompt": INITIAL_PROMPT}
25
 
26
  @fastapi_app.get("/main_prompt")
27
- def get_main_prompt():
28
  return {"prompt": MAIN_PROMPT}
29
 
30
  # ✅ Chatbot Function
@@ -66,37 +66,36 @@ def respond(user_message, history):
66
  return "", history
67
 
68
  # ✅ Gradio UI
69
- with gr.Blocks() as gradio_app:
70
- gr.Markdown("## Simple Chat Interface")
71
-
72
- chatbot = gr.Chatbot(
73
- value=[{"role": "assistant", "content": INITIAL_PROMPT}],
74
- height=500,
75
- type="messages"
76
- )
77
-
78
- state_history = gr.State([("", INITIAL_PROMPT)])
79
-
80
- user_input = gr.Textbox(
81
- placeholder="Type your message here...",
82
- label="Your Input"
83
- )
84
-
85
- user_input.submit(
86
- respond,
87
- inputs=[user_input, state_history],
88
- outputs=[user_input, chatbot]
89
- ).then(
90
- fn=lambda _, h: h,
91
- inputs=[user_input, chatbot],
92
- outputs=[state_history]
93
- )
94
-
95
- # ✅ Run FastAPI and Gradio Together
96
- def run_gradio():
97
- gradio_app.launch(server_name="0.0.0.0", server_port=7860)
98
-
99
- threading.Thread(target=run_gradio).start()
100
-
101
  if __name__ == "__main__":
 
102
  uvicorn.run(fastapi_app, host="0.0.0.0", port=8000)
 
20
  fastapi_app = FastAPI()
21
 
22
  @fastapi_app.get("/initial_prompt")
23
+ async def get_initial_prompt():
24
  return {"prompt": INITIAL_PROMPT}
25
 
26
  @fastapi_app.get("/main_prompt")
27
+ async def get_main_prompt():
28
  return {"prompt": MAIN_PROMPT}
29
 
30
  # ✅ Chatbot Function
 
66
  return "", history
67
 
68
  # ✅ Gradio UI
69
+ def launch_gradio():
70
+ with gr.Blocks() as gradio_app:
71
+ gr.Markdown("## Simple Chat Interface")
72
+
73
+ chatbot = gr.Chatbot(
74
+ value=[{"role": "assistant", "content": INITIAL_PROMPT}],
75
+ height=500,
76
+ type="messages"
77
+ )
78
+
79
+ state_history = gr.State([("", INITIAL_PROMPT)])
80
+
81
+ user_input = gr.Textbox(
82
+ placeholder="Type your message here...",
83
+ label="Your Input"
84
+ )
85
+
86
+ user_input.submit(
87
+ respond,
88
+ inputs=[user_input, state_history],
89
+ outputs=[user_input, chatbot]
90
+ ).then(
91
+ fn=lambda _, h: h,
92
+ inputs=[user_input, chatbot],
93
+ outputs=[state_history]
94
+ )
95
+
96
+ gradio_app.launch(server_name="0.0.0.0", server_port=7860, show_error=True, enable_queue=True)
97
+
98
+ # ✅ Run FastAPI and Gradio Separately
 
 
99
  if __name__ == "__main__":
100
+ threading.Thread(target=launch_gradio, daemon=True).start()
101
  uvicorn.run(fastapi_app, host="0.0.0.0", port=8000)