Spaces:
Sleeping
Sleeping
python verbose run command in dockerfile fixes output hang; minor fixes for mcp documention
Browse files- Dockerfile +3 -3
- app/main.py +56 -25
- requirements.txt +3 -7
Dockerfile
CHANGED
|
@@ -8,10 +8,10 @@ COPY requirements.txt .
|
|
| 8 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 9 |
|
| 10 |
# copy app
|
| 11 |
-
COPY app
|
| 12 |
|
| 13 |
# expose Gradio default port
|
| 14 |
EXPOSE 7860
|
| 15 |
|
| 16 |
-
# launch
|
| 17 |
-
CMD ["python", "app.py"]
|
|
|
|
| 8 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 9 |
|
| 10 |
# copy app
|
| 11 |
+
COPY app ./app
|
| 12 |
|
| 13 |
# expose Gradio default port
|
| 14 |
EXPOSE 7860
|
| 15 |
|
| 16 |
+
# launch with unbuffered output
|
| 17 |
+
CMD ["python", "-u", "app/main.py"]
|
app/main.py
CHANGED
|
@@ -1,49 +1,80 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_client import Client
|
| 3 |
-
from langgraph import
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
client = Client("giz/chatfed_retriever") # HF repo name
|
| 8 |
-
|
| 9 |
-
query=query,
|
| 10 |
reports_filter="",
|
| 11 |
sources_filter="",
|
| 12 |
subtype_filter="",
|
| 13 |
year_filter="",
|
| 14 |
api_name="/retrieve"
|
| 15 |
)
|
|
|
|
| 16 |
|
| 17 |
-
# node 2:
|
| 18 |
-
def generate_node(
|
| 19 |
client = Client("giz/chatfed_generator")
|
| 20 |
-
|
| 21 |
-
query=query,
|
| 22 |
-
context=context,
|
| 23 |
api_name="/generate"
|
| 24 |
)
|
|
|
|
| 25 |
|
| 26 |
# build the graph
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
# expose a simple Gradio interface that drives the graph
|
| 33 |
def pipeline(query: str):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
fn=pipeline,
|
| 42 |
-
inputs=gr.Textbox(lines=2, placeholder="Enter
|
| 43 |
outputs="text",
|
| 44 |
-
title="
|
| 45 |
)
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_client import Client
|
| 3 |
+
from langgraph.graph import StateGraph, START, END
|
| 4 |
+
from typing import TypedDict
|
| 5 |
|
| 6 |
+
# Define the state schema
|
| 7 |
+
class GraphState(TypedDict):
|
| 8 |
+
query: str
|
| 9 |
+
context: str
|
| 10 |
+
result: str
|
| 11 |
+
|
| 12 |
+
# node 1: retriever
|
| 13 |
+
def retrieve_node(state: GraphState) -> GraphState:
|
| 14 |
client = Client("giz/chatfed_retriever") # HF repo name
|
| 15 |
+
context = client.predict(
|
| 16 |
+
query=state["query"],
|
| 17 |
reports_filter="",
|
| 18 |
sources_filter="",
|
| 19 |
subtype_filter="",
|
| 20 |
year_filter="",
|
| 21 |
api_name="/retrieve"
|
| 22 |
)
|
| 23 |
+
return {"context": context}
|
| 24 |
|
| 25 |
+
# node 2: generator
|
| 26 |
+
def generate_node(state: GraphState) -> GraphState:
|
| 27 |
client = Client("giz/chatfed_generator")
|
| 28 |
+
result = client.predict(
|
| 29 |
+
query=state["query"],
|
| 30 |
+
context=state["context"],
|
| 31 |
api_name="/generate"
|
| 32 |
)
|
| 33 |
+
return {"result": result}
|
| 34 |
|
| 35 |
# build the graph
|
| 36 |
+
workflow = StateGraph(GraphState)
|
| 37 |
+
|
| 38 |
+
# Add nodes
|
| 39 |
+
workflow.add_node("retrieve", retrieve_node)
|
| 40 |
+
workflow.add_node("generate", generate_node)
|
| 41 |
+
|
| 42 |
+
# Add edges
|
| 43 |
+
workflow.add_edge(START, "retrieve")
|
| 44 |
+
workflow.add_edge("retrieve", "generate")
|
| 45 |
+
workflow.add_edge("generate", END)
|
| 46 |
+
|
| 47 |
+
# Compile the graph
|
| 48 |
+
graph = workflow.compile()
|
| 49 |
|
|
|
|
| 50 |
def pipeline(query: str):
|
| 51 |
+
"""
|
| 52 |
+
Execute the ChatFed orchestration pipeline to process a user query.
|
| 53 |
+
|
| 54 |
+
This function orchestrates a two-step workflow:
|
| 55 |
+
1. Retrieve relevant context using the ChatFed retriever service
|
| 56 |
+
2. Generate a response using the ChatFed generator service with the retrieved context
|
| 57 |
+
|
| 58 |
+
Args:
|
| 59 |
+
query (str): The user's input query/question to be processed
|
| 60 |
+
|
| 61 |
+
Returns:
|
| 62 |
+
str: The generated response from the ChatFed generator service
|
| 63 |
+
"""
|
| 64 |
+
# run the graph with the initial state
|
| 65 |
+
initial_state = {"query": query, "context": "", "result": ""}
|
| 66 |
+
final_state = graph.invoke(initial_state)
|
| 67 |
+
return final_state["result"]
|
| 68 |
|
| 69 |
+
ui = gr.Interface(
|
| 70 |
fn=pipeline,
|
| 71 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter query here"),
|
| 72 |
outputs="text",
|
| 73 |
+
title="ChatFed Orchestrator",
|
| 74 |
)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
+
ui.launch(server_name="0.0.0.0",
|
| 78 |
+
server_port=7860,
|
| 79 |
+
mcp_server=True,
|
| 80 |
+
show_error=True)
|
requirements.txt
CHANGED
|
@@ -1,8 +1,4 @@
|
|
| 1 |
gradio[mcp]
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
sentence-transformers
|
| 6 |
-
gradio_client>=0.10.0
|
| 7 |
-
huggingface_hub>=0.20.0
|
| 8 |
-
torch
|
|
|
|
| 1 |
gradio[mcp]
|
| 2 |
+
gradio_client>=1.0.0
|
| 3 |
+
langgraph>=0.2.0
|
| 4 |
+
|
|
|
|
|
|
|
|
|
|
|
|