Spaces:
Build error
Build error
Temporarily disable HuggingFace model integration and simplify FastAPI endpoints
Browse files- Comment out HuggingFace client initialization
- Remove model calling and graph configuration
- Simplify root endpoint with more descriptive function name
- Comment out generate and test-token endpoints
app.py
CHANGED
|
@@ -6,93 +6,93 @@ from langchain_core.messages import HumanMessage, AIMessage
|
|
| 6 |
from langgraph.checkpoint.memory import MemorySaver
|
| 7 |
from langgraph.graph import START, MessagesState, StateGraph
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
client = InferenceClient(
|
| 11 |
-
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
# Define the graph
|
| 15 |
-
workflow = StateGraph(state_schema=MessagesState)
|
| 16 |
|
| 17 |
# Define the function that calls the model
|
| 18 |
-
def call_model(state: MessagesState):
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
workflow.add_edge(START, "model")
|
| 41 |
-
workflow.add_node("model", call_model)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
memory = MemorySaver()
|
| 45 |
-
graph_app = workflow.compile(checkpointer=memory)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
class QueryRequest(BaseModel):
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
# Create the FastAPI application
|
| 53 |
app = FastAPI(title="LangChain FastAPI", description="API to generate text using LangChain and LangGraph")
|
| 54 |
|
| 55 |
@app.get("/")
|
| 56 |
-
async def
|
| 57 |
"""Welcome endpoint"""
|
| 58 |
return {"detail": "Welcome to FastAPI, Langchain, Docker tutorial"}
|
| 59 |
|
| 60 |
-
@app.post("/generate")
|
| 61 |
-
async def generate(request: QueryRequest):
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
|
| 83 |
# Add an endpoint to test the token directly
|
| 84 |
-
@app.get("/test-token")
|
| 85 |
-
async def test_token():
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
import uvicorn
|
|
|
|
| 6 |
from langgraph.checkpoint.memory import MemorySaver
|
| 7 |
from langgraph.graph import START, MessagesState, StateGraph
|
| 8 |
|
| 9 |
+
# Initialize the HuggingFace client
|
| 10 |
+
# client = InferenceClient(
|
| 11 |
+
# model="HuggingFaceTB/SmolLM2-1.7B-Instruct",
|
| 12 |
+
# )
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Define the function that calls the model
|
| 15 |
+
# def call_model(state: MessagesState):
|
| 16 |
+
# # Convert LangChain messages to HuggingFace format
|
| 17 |
+
# hf_messages = []
|
| 18 |
+
# for msg in state["messages"]:
|
| 19 |
+
# if isinstance(msg, HumanMessage):
|
| 20 |
+
# hf_messages.append({"role": "user", "content": msg.content})
|
| 21 |
+
# elif isinstance(msg, AIMessage):
|
| 22 |
+
# hf_messages.append({"role": "assistant", "content": msg.content})
|
| 23 |
|
| 24 |
+
# # Call the API
|
| 25 |
+
# response = client.chat_completion(
|
| 26 |
+
# messages=hf_messages,
|
| 27 |
+
# temperature=0.5,
|
| 28 |
+
# max_tokens=64,
|
| 29 |
+
# top_p=0.7
|
| 30 |
+
# )
|
| 31 |
|
| 32 |
+
# # Convert the response to LangChain format
|
| 33 |
+
# ai_message = AIMessage(content=response.choices[0].message.content)
|
| 34 |
+
# return {"messages": state["messages"] + [ai_message]}
|
| 35 |
+
|
| 36 |
+
# Define the graph
|
| 37 |
+
# workflow = StateGraph(state_schema=MessagesState)
|
| 38 |
|
| 39 |
+
# Define the node in the graph
|
| 40 |
+
# workflow.add_edge(START, "model")
|
| 41 |
+
# workflow.add_node("model", call_model)
|
| 42 |
|
| 43 |
+
# Add memory
|
| 44 |
+
# memory = MemorySaver()
|
| 45 |
+
# graph_app = workflow.compile(checkpointer=memory)
|
| 46 |
|
| 47 |
+
# Define the data model for the request
|
| 48 |
+
# class QueryRequest(BaseModel):
|
| 49 |
+
# query: str
|
| 50 |
+
# thread_id: str = "default"
|
| 51 |
|
| 52 |
# Create the FastAPI application
|
| 53 |
app = FastAPI(title="LangChain FastAPI", description="API to generate text using LangChain and LangGraph")
|
| 54 |
|
| 55 |
@app.get("/")
|
| 56 |
+
async def api_home():
|
| 57 |
"""Welcome endpoint"""
|
| 58 |
return {"detail": "Welcome to FastAPI, Langchain, Docker tutorial"}
|
| 59 |
|
| 60 |
+
# @app.post("/generate")
|
| 61 |
+
# async def generate(request: QueryRequest):
|
| 62 |
+
# """Endpoint to generate text using the language model"""
|
| 63 |
+
# try:
|
| 64 |
+
# # Configure the thread ID
|
| 65 |
+
# config = {"configurable": {"thread_id": request.thread_id}}
|
| 66 |
|
| 67 |
+
# # Create the input message
|
| 68 |
+
# input_messages = [HumanMessage(content=request.query)]
|
| 69 |
|
| 70 |
+
# # Invoke the graph
|
| 71 |
+
# output = graph_app.invoke({"messages": input_messages}, config)
|
| 72 |
|
| 73 |
+
# # Get the model response
|
| 74 |
+
# response = output["messages"][-1].content
|
| 75 |
|
| 76 |
+
# return {
|
| 77 |
+
# "generated_text": response,
|
| 78 |
+
# "thread_id": request.thread_id
|
| 79 |
+
# }
|
| 80 |
+
# except Exception as e:
|
| 81 |
+
# raise HTTPException(status_code=500, detail=f"Error al generar texto: {str(e)}")
|
| 82 |
|
| 83 |
# Add an endpoint to test the token directly
|
| 84 |
+
# @app.get("/test-token")
|
| 85 |
+
# async def test_token():
|
| 86 |
+
# """Endpoint to test the authentication with HuggingFace"""
|
| 87 |
+
# try:
|
| 88 |
+
# # Make a simple request to verify that the token works
|
| 89 |
+
# response = client.chat_completion(
|
| 90 |
+
# messages=[{"role": "user", "content": "Hello"}],
|
| 91 |
+
# max_tokens=10
|
| 92 |
+
# )
|
| 93 |
+
# return {"status": "success", "message": "Token is valid", "response": response.choices[0].message.content}
|
| 94 |
+
# except Exception as e:
|
| 95 |
+
# return {"status": "error", "message": str(e)}
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
import uvicorn
|