Update app.py
Browse files
app.py
CHANGED
@@ -330,24 +330,20 @@ try:
|
|
330 |
except Exception as e:
|
331 |
print(f"[ERROR] Workflow execution failed: {e}")
|
332 |
'''
|
333 |
-
|
334 |
-
from
|
335 |
-
import functools
|
336 |
from langgraph.graph import StateGraph, END
|
|
|
337 |
from langchain_core.messages import HumanMessage
|
338 |
-
from langchain_community.tools.tavily_search import TavilySearchResults
|
339 |
-
from langchain_experimental.tools import PythonREPLTool
|
340 |
from langchain_huggingface import HuggingFacePipeline
|
341 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
342 |
import gradio as gr
|
343 |
|
344 |
-
# Define
|
345 |
def create_llm():
|
346 |
-
"""Create the HuggingFace LLM pipeline."""
|
347 |
model_name = "Qwen/Qwen2.5-7B-Instruct-1M"
|
348 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
349 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
350 |
-
|
351 |
llm_pipeline = pipeline(
|
352 |
task="text-generation",
|
353 |
model=model,
|
@@ -358,19 +354,13 @@ def create_llm():
|
|
358 |
return HuggingFacePipeline(pipeline=llm_pipeline)
|
359 |
|
360 |
# Mock Tools for Registration and Scheduling
|
361 |
-
|
362 |
-
scheduling_tool = PythonREPLTool() # A mock scheduling tool
|
363 |
-
|
364 |
-
# Define the agents
|
365 |
-
def registration_agent(visitor_details: Dict[str, str]) -> Dict[str, Any]:
|
366 |
"""Check if the visitor is registered."""
|
367 |
visitor_name = visitor_details.get("visitor_name")
|
368 |
visitor_mobile = visitor_details.get("visitor_mobile")
|
369 |
|
370 |
-
# Mock registration lookup
|
371 |
registered_visitors = [{"visitor_name": "John Doe", "visitor_mobile": "1234567890"}]
|
372 |
-
|
373 |
-
# Check if visitor exists
|
374 |
is_registered = any(
|
375 |
v["visitor_name"] == visitor_name and v["visitor_mobile"] == visitor_mobile
|
376 |
for v in registered_visitors
|
@@ -382,75 +372,75 @@ def scheduling_agent(scheduling_details: Dict[str, str]) -> Dict[str, str]:
|
|
382 |
doctor_name = scheduling_details.get("doctor_name")
|
383 |
department_name = scheduling_details.get("department_name")
|
384 |
|
385 |
-
# Mock scheduling logic
|
386 |
appointment_status = "Scheduled successfully"
|
387 |
return {"status": appointment_status, "doctor_name": doctor_name, "department_name": department_name}
|
388 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
389 |
|
390 |
-
|
391 |
-
|
392 |
-
messages:
|
393 |
-
|
394 |
-
|
395 |
-
def input_state(state):
|
396 |
-
"""State to input visitor details."""
|
397 |
-
return {"messages": [HumanMessage(content="Please provide your name and mobile number.")]}
|
398 |
|
399 |
-
def registration_state(state):
|
400 |
"""State to check visitor registration."""
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
if result["registered"]:
|
406 |
return {"messages": ["Visitor is registered."], "next": "SchedulingState"}
|
407 |
else:
|
408 |
return {"messages": ["Visitor not found in records. Please register first."], "next": END}
|
409 |
|
410 |
-
def scheduling_state(state):
|
411 |
-
"""State
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
return {
|
417 |
-
"messages": [
|
418 |
-
|
|
|
|
|
419 |
}
|
420 |
|
421 |
-
|
422 |
-
# Build Langgraph Workflow
|
423 |
workflow = StateGraph(VisitorState)
|
424 |
-
|
425 |
-
# Add nodes
|
426 |
workflow.add_node("InputState", input_state)
|
427 |
workflow.add_node("RegistrationState", registration_state)
|
428 |
workflow.add_node("SchedulingState", scheduling_state)
|
429 |
|
430 |
# Define edges
|
431 |
workflow.add_edge("InputState", "RegistrationState")
|
432 |
-
workflow.add_edge("RegistrationState", "SchedulingState")
|
433 |
workflow.add_conditional_edges(
|
434 |
"RegistrationState",
|
435 |
-
lambda
|
436 |
-
{
|
|
|
|
|
|
|
437 |
)
|
438 |
-
|
439 |
workflow.set_entry_point("InputState")
|
440 |
compiled_graph = workflow.compile()
|
441 |
|
442 |
# Gradio Frontend
|
443 |
def gradio_interface(visitor_name, visitor_mobile, doctor_name, department_name):
|
444 |
-
"""Gradio interface
|
445 |
-
state =
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
|
452 |
-
#
|
453 |
-
result = compiled_graph.invoke(state)
|
454 |
return result["messages"][0]
|
455 |
|
456 |
iface = gr.Interface(
|
|
|
330 |
except Exception as e:
|
331 |
print(f"[ERROR] Workflow execution failed: {e}")
|
332 |
'''
|
333 |
+
from typing import Dict
|
334 |
+
from pydantic import BaseModel
|
|
|
335 |
from langgraph.graph import StateGraph, END
|
336 |
+
from langchain_core.prompts import PromptTemplate
|
337 |
from langchain_core.messages import HumanMessage
|
|
|
|
|
338 |
from langchain_huggingface import HuggingFacePipeline
|
339 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
340 |
import gradio as gr
|
341 |
|
342 |
+
# Define HuggingFace LLM
|
343 |
def create_llm():
|
|
|
344 |
model_name = "Qwen/Qwen2.5-7B-Instruct-1M"
|
345 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
346 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
347 |
llm_pipeline = pipeline(
|
348 |
task="text-generation",
|
349 |
model=model,
|
|
|
354 |
return HuggingFacePipeline(pipeline=llm_pipeline)
|
355 |
|
356 |
# Mock Tools for Registration and Scheduling
|
357 |
+
def registration_agent(visitor_details: Dict[str, str]) -> Dict[str, bool]:
|
|
|
|
|
|
|
|
|
358 |
"""Check if the visitor is registered."""
|
359 |
visitor_name = visitor_details.get("visitor_name")
|
360 |
visitor_mobile = visitor_details.get("visitor_mobile")
|
361 |
|
362 |
+
# Mock registration lookup
|
363 |
registered_visitors = [{"visitor_name": "John Doe", "visitor_mobile": "1234567890"}]
|
|
|
|
|
364 |
is_registered = any(
|
365 |
v["visitor_name"] == visitor_name and v["visitor_mobile"] == visitor_mobile
|
366 |
for v in registered_visitors
|
|
|
372 |
doctor_name = scheduling_details.get("doctor_name")
|
373 |
department_name = scheduling_details.get("department_name")
|
374 |
|
375 |
+
# Mock scheduling logic
|
376 |
appointment_status = "Scheduled successfully"
|
377 |
return {"status": appointment_status, "doctor_name": doctor_name, "department_name": department_name}
|
378 |
|
379 |
+
# Define the VisitorState schema
|
380 |
+
class VisitorState(BaseModel):
|
381 |
+
visitor_name: str = ""
|
382 |
+
visitor_mobile: str = ""
|
383 |
+
doctor_name: str = ""
|
384 |
+
department_name: str = ""
|
385 |
+
messages: list = []
|
386 |
|
387 |
+
def input_state(state: VisitorState) -> dict:
|
388 |
+
"""Initial state to collect visitor details."""
|
389 |
+
return {"messages": ["Please provide your name and mobile number."], "next": "RegistrationState"}
|
|
|
|
|
|
|
|
|
|
|
390 |
|
391 |
+
def registration_state(state: VisitorState) -> dict:
|
392 |
"""State to check visitor registration."""
|
393 |
+
result = registration_agent(
|
394 |
+
{"visitor_name": state.visitor_name, "visitor_mobile": state.visitor_mobile}
|
395 |
+
)
|
|
|
396 |
if result["registered"]:
|
397 |
return {"messages": ["Visitor is registered."], "next": "SchedulingState"}
|
398 |
else:
|
399 |
return {"messages": ["Visitor not found in records. Please register first."], "next": END}
|
400 |
|
401 |
+
def scheduling_state(state: VisitorState) -> dict:
|
402 |
+
"""State to schedule an appointment."""
|
403 |
+
result = scheduling_agent(
|
404 |
+
{"doctor_name": state.doctor_name, "department_name": state.department_name}
|
405 |
+
)
|
|
|
406 |
return {
|
407 |
+
"messages": [
|
408 |
+
f"Appointment {result['status']} with Dr.{result['doctor_name']} in {result['department_name']} department."
|
409 |
+
],
|
410 |
+
"next": END,
|
411 |
}
|
412 |
|
413 |
+
# Define Workflow
|
|
|
414 |
workflow = StateGraph(VisitorState)
|
|
|
|
|
415 |
workflow.add_node("InputState", input_state)
|
416 |
workflow.add_node("RegistrationState", registration_state)
|
417 |
workflow.add_node("SchedulingState", scheduling_state)
|
418 |
|
419 |
# Define edges
|
420 |
workflow.add_edge("InputState", "RegistrationState")
|
|
|
421 |
workflow.add_conditional_edges(
|
422 |
"RegistrationState",
|
423 |
+
lambda state: state.get("next"),
|
424 |
+
{
|
425 |
+
"SchedulingState": "SchedulingState",
|
426 |
+
END: END,
|
427 |
+
}
|
428 |
)
|
|
|
429 |
workflow.set_entry_point("InputState")
|
430 |
compiled_graph = workflow.compile()
|
431 |
|
432 |
# Gradio Frontend
|
433 |
def gradio_interface(visitor_name, visitor_mobile, doctor_name, department_name):
|
434 |
+
"""Gradio interface function."""
|
435 |
+
state = VisitorState(
|
436 |
+
visitor_name=visitor_name,
|
437 |
+
visitor_mobile=visitor_mobile,
|
438 |
+
doctor_name=doctor_name,
|
439 |
+
department_name=department_name,
|
440 |
+
)
|
441 |
|
442 |
+
# Execute workflow
|
443 |
+
result = compiled_graph.invoke(state.dict())
|
444 |
return result["messages"][0]
|
445 |
|
446 |
iface = gr.Interface(
|