Update app.py
Browse files
app.py
CHANGED
@@ -641,11 +641,12 @@ def build_demo():
|
|
641 |
return demo
|
642 |
# --- API endpoint: /chat ---
|
643 |
# --- API endpoint: /chat ---
|
|
|
|
|
|
|
644 |
|
|
|
645 |
def http_api_infer(question: str):
|
646 |
-
# NOTE: No request here – Gradio will not inject it automatically
|
647 |
-
# So remove token checking (or add later via FastAPI if needed)
|
648 |
-
|
649 |
dummy_state = init_state()
|
650 |
dummy_state.set_system_message("You are a helpful assistant.")
|
651 |
dummy_state.append_message(Conversation.USER, question)
|
@@ -678,8 +679,7 @@ def http_api_infer(question: str):
|
|
678 |
except Exception as e:
|
679 |
return f"❌ Error: {str(e)}"
|
680 |
|
681 |
-
|
682 |
-
# ✅ Define the API Interface after the function is declared
|
683 |
api_interface = gr.Interface(
|
684 |
fn=http_api_infer,
|
685 |
inputs=[gr.Textbox(label="Question")],
|
@@ -687,33 +687,20 @@ api_interface = gr.Interface(
|
|
687 |
api_name="/chat"
|
688 |
)
|
689 |
|
|
|
|
|
690 |
|
691 |
-
|
692 |
-
|
693 |
-
parser.add_argument("--host", type=str, default="0.0.0.0")
|
694 |
-
parser.add_argument("--port", type=int, default=7860)
|
695 |
-
parser.add_argument("--concurrency-count", type=int, default=10)
|
696 |
-
parser.add_argument("--share", action="store_true")
|
697 |
-
parser.add_argument("--moderate", action="store_true")
|
698 |
-
args = parser.parse_args()
|
699 |
-
logger.info(f"args: {args}")
|
700 |
-
|
701 |
-
# UI block
|
702 |
-
ui_demo = build_demo()
|
703 |
-
|
704 |
-
# Combine UI + API endpoint
|
705 |
-
demo = gr.TabbedInterface(
|
706 |
-
interface_list=[ui_demo, api_interface],
|
707 |
-
tab_names=["UI", "API"]
|
708 |
-
)
|
709 |
|
710 |
-
|
711 |
-
|
712 |
-
server_name=args.host,
|
713 |
-
server_port=args.port,
|
714 |
-
share=args.share,
|
715 |
-
max_threads=args.concurrency_count,
|
716 |
-
)
|
717 |
|
|
|
|
|
|
|
|
|
|
|
|
|
718 |
|
719 |
|
|
|
641 |
return demo
|
642 |
# --- API endpoint: /chat ---
|
643 |
# --- API endpoint: /chat ---
|
644 |
+
from fastapi import FastAPI
|
645 |
+
import gradio as gr
|
646 |
+
import uvicorn
|
647 |
|
648 |
+
# --- API function ---
|
649 |
def http_api_infer(question: str):
|
|
|
|
|
|
|
650 |
dummy_state = init_state()
|
651 |
dummy_state.set_system_message("You are a helpful assistant.")
|
652 |
dummy_state.append_message(Conversation.USER, question)
|
|
|
679 |
except Exception as e:
|
680 |
return f"❌ Error: {str(e)}"
|
681 |
|
682 |
+
# Gradio API Interface
|
|
|
683 |
api_interface = gr.Interface(
|
684 |
fn=http_api_infer,
|
685 |
inputs=[gr.Textbox(label="Question")],
|
|
|
687 |
api_name="/chat"
|
688 |
)
|
689 |
|
690 |
+
# UI block
|
691 |
+
ui_demo = build_demo()
|
692 |
|
693 |
+
# Setup FastAPI app
|
694 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
695 |
|
696 |
+
# Mount UI
|
697 |
+
app = gr.mount_gradio_app(app, ui_demo, path="/")
|
|
|
|
|
|
|
|
|
|
|
698 |
|
699 |
+
# Mount API
|
700 |
+
app = gr.mount_gradio_app(app, api_interface, path="/api")
|
701 |
+
|
702 |
+
# Run
|
703 |
+
if __name__ == "__main__":
|
704 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
705 |
|
706 |
|