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@ def greet(n):
|
|
11 |
return f"Hello {zero + n} Tensor"
|
12 |
|
13 |
demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text())
|
14 |
-
demo.launch()
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
2 |
+
from langchain_huggingface import HuggingFacePipeline
|
3 |
+
from langchain.tools import Tool
|
4 |
+
from langchain.agents import create_react_agent
|
5 |
+
from langgraph.graph import StateGraph, END
|
6 |
+
from pydantic import BaseModel
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# ---------------------------------------
|
10 |
+
# Step 1: Define Hugging Face LLM (Qwen/Qwen2.5-7B-Instruct-1M)
|
11 |
+
# ---------------------------------------
|
12 |
+
def create_llm():
|
13 |
+
model_name = "Qwen/Qwen2.5-7B-Instruct-1M"
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
15 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
16 |
+
|
17 |
+
llm_pipeline = pipeline(
|
18 |
+
task="text-generation",
|
19 |
+
model=model,
|
20 |
+
tokenizer=tokenizer,
|
21 |
+
device=-1, # CPU mode, set to 0 for GPU
|
22 |
+
max_new_tokens=200
|
23 |
+
)
|
24 |
+
return HuggingFacePipeline(pipeline=llm_pipeline)
|
25 |
+
|
26 |
+
# ---------------------------------------
|
27 |
+
# Step 2: Create Agents
|
28 |
+
# ---------------------------------------
|
29 |
+
llm = create_llm()
|
30 |
+
|
31 |
+
# Registration Agent
|
32 |
+
registration_agent = Tool(
|
33 |
+
name="registration_check",
|
34 |
+
description="Check if a patient is registered.",
|
35 |
+
func=lambda details: registration_tool(details.get("visitor_name"), details.get("visitor_mobile"))
|
36 |
+
)
|
37 |
+
|
38 |
+
# Scheduling Agent
|
39 |
+
scheduling_agent = Tool(
|
40 |
+
name="schedule_appointment",
|
41 |
+
description="Fetch available time slots for a doctor.",
|
42 |
+
func=lambda details: doctor_slots_tool(details.get("doctor_name"))
|
43 |
+
)
|
44 |
+
|
45 |
+
# Payment Agent
|
46 |
+
payment_agent = Tool(
|
47 |
+
name="process_payment",
|
48 |
+
description="Generate a payment link and confirm the payment.",
|
49 |
+
func=lambda details: confirm_payment_tool(details.get("transaction_id"))
|
50 |
+
)
|
51 |
+
|
52 |
+
# Email Agent
|
53 |
+
email_agent = Tool(
|
54 |
+
name="send_email",
|
55 |
+
description="Send appointment confirmation email to the visitor.",
|
56 |
+
func=lambda details: email_tool(
|
57 |
+
details.get("visitor_email"),
|
58 |
+
details.get("appointment_details"),
|
59 |
+
details.get("hospital_location")
|
60 |
+
)
|
61 |
+
)
|
62 |
+
|
63 |
+
# ---------------------------------------
|
64 |
+
# Step 3: Tools and Mock Functions
|
65 |
+
# ---------------------------------------
|
66 |
+
def registration_tool(visitor_name: str, visitor_mobile: str) -> bool:
|
67 |
+
registered_visitors = [{"visitor_name": "John Doe", "visitor_mobile": "1234567890"}]
|
68 |
+
return any(
|
69 |
+
v["visitor_name"] == visitor_name and v["visitor_mobile"] == visitor_mobile
|
70 |
+
for v in registered_visitors
|
71 |
+
)
|
72 |
+
|
73 |
+
def register_visitor(visitor_name: str, visitor_mobile: str) -> bool:
|
74 |
+
"""Register a new user if not already registered."""
|
75 |
+
return True # Simulate successful registration
|
76 |
+
|
77 |
+
def doctor_slots_tool(doctor_name: str):
|
78 |
+
available_slots = {
|
79 |
+
"Dr. Smith": ["10:00 AM", "2:00 PM"],
|
80 |
+
"Dr. Brown": ["12:00 PM"]
|
81 |
+
}
|
82 |
+
return available_slots.get(doctor_name, [])
|
83 |
+
|
84 |
+
def payment_tool(amount: float):
|
85 |
+
"""Generate a payment link."""
|
86 |
+
return f"http://mock-payment-link.com/pay?amount={amount}"
|
87 |
+
|
88 |
+
def confirm_payment_tool(transaction_id: str) -> dict:
|
89 |
+
"""Confirm the payment."""
|
90 |
+
if transaction_id == "TIMEOUT":
|
91 |
+
return {"status": "FAILED", "reason_code": "timeout"}
|
92 |
+
elif transaction_id == "SUCCESS":
|
93 |
+
return {"status": "SUCCESS", "reason_code": None}
|
94 |
+
else:
|
95 |
+
return {"status": "FAILED", "reason_code": "other_error"}
|
96 |
+
|
97 |
+
def email_tool(visitor_email: str, appointment_details: str, hospital_location: str) -> bool:
|
98 |
+
"""Simulate sending an email to the visitor with appointment details."""
|
99 |
+
print(f"Sending email to {visitor_email}...")
|
100 |
+
print(f"Appointment Details: {appointment_details}")
|
101 |
+
print(f"Hospital Location: {hospital_location}")
|
102 |
+
# Simulate success
|
103 |
+
return True
|
104 |
+
|
105 |
+
# ---------------------------------------
|
106 |
+
# Step 4: Define Workflow States
|
107 |
+
# ---------------------------------------
|
108 |
+
class VisitorState(BaseModel):
|
109 |
+
visitor_name: str = ""
|
110 |
+
visitor_mobile: str = ""
|
111 |
+
visitor_email: str = ""
|
112 |
+
doctor_name: str = ""
|
113 |
+
department_name: str = ""
|
114 |
+
selected_slot: str = ""
|
115 |
+
messages: list = []
|
116 |
+
payment_confirmed: bool = False
|
117 |
+
email_sent: bool = False
|
118 |
+
|
119 |
+
def input_state(state: VisitorState):
|
120 |
+
"""InputState: Collect visitor details."""
|
121 |
+
return {"messages": ["Please provide your name, mobile number, and email."], "next": "RegistrationState"}
|
122 |
+
|
123 |
+
def registration_state(state: VisitorState):
|
124 |
+
"""Registration State: Check and register visitor."""
|
125 |
+
is_registered = registration_tool(state.visitor_name, state.visitor_mobile)
|
126 |
+
if is_registered:
|
127 |
+
return {"messages": ["Visitor is registered."], "next": "SchedulingState"}
|
128 |
+
else:
|
129 |
+
successfully_registered = register_visitor(state.visitor_name, state.visitor_mobile)
|
130 |
+
if successfully_registered:
|
131 |
+
return {"messages": ["Visitor has been successfully registered."], "next": "SchedulingState"}
|
132 |
+
else:
|
133 |
+
return {"messages": ["Registration failed. Please try again later."], "next": END}
|
134 |
+
|
135 |
+
def scheduling_state(state: VisitorState):
|
136 |
+
"""SchedulingState: Fetch available slots for a doctor."""
|
137 |
+
available_slots = doctor_slots_tool(state.doctor_name)
|
138 |
+
if available_slots:
|
139 |
+
state.selected_slot = available_slots[0]
|
140 |
+
return {"messages": [f"Slot selected for {state.doctor_name}: {state.selected_slot}"], "next": "PaymentState"}
|
141 |
+
else:
|
142 |
+
return {"messages": [f"No available slots for {state.doctor_name}."], "next": END}
|
143 |
+
|
144 |
+
def payment_state(state: VisitorState):
|
145 |
+
"""PaymentState: Generate payment link and confirm."""
|
146 |
+
payment_link = payment_tool(500)
|
147 |
+
state.messages.append(f"Please proceed to pay at: {payment_link}")
|
148 |
+
|
149 |
+
# Simulate payment confirmation
|
150 |
+
payment_response = confirm_payment_tool("SUCCESS")
|
151 |
+
if payment_response["status"] == "SUCCESS":
|
152 |
+
state.payment_confirmed = True
|
153 |
+
return {"messages": ["Payment successful. Appointment is being finalized."], "next": "FinalState"}
|
154 |
+
elif payment_response["reason_code"] == "timeout":
|
155 |
+
return {"messages": ["Payment timed out. Retrying payment..."], "next": "PaymentState"}
|
156 |
+
else:
|
157 |
+
return {"messages": ["Payment failed due to an error. Please try again later."], "next": END}
|
158 |
+
|
159 |
+
def final_state(state: VisitorState):
|
160 |
+
"""FinalState: Send email confirmation and finalize the appointment."""
|
161 |
+
if state.payment_confirmed:
|
162 |
+
appointment_details = f"Doctor: {state.doctor_name}\nTime: {state.selected_slot}"
|
163 |
+
hospital_location = "123 Main St, Springfield, USA"
|
164 |
+
email_success = email_tool(state.visitor_email, appointment_details, hospital_location)
|
165 |
+
|
166 |
+
if email_success:
|
167 |
+
state.email_sent = True
|
168 |
+
return {"messages": [f"Appointment confirmed. Details sent to your email: {state.visitor_email}"], "next": END}
|
169 |
+
else:
|
170 |
+
return {"messages": ["Appointment confirmed, but failed to send email. Please contact support."], "next": END}
|
171 |
+
else:
|
172 |
+
return {"messages": ["Payment confirmation failed. Appointment could not be finalized."], "next": END}
|
173 |
+
|
174 |
+
# ---------------------------------------
|
175 |
+
# Step 5: Build Langgraph Workflow
|
176 |
+
# ---------------------------------------
|
177 |
+
workflow = StateGraph(VisitorState)
|
178 |
+
|
179 |
+
# Add nodes
|
180 |
+
workflow.add_node("InputState", input_state)
|
181 |
+
workflow.add_node("RegistrationState", registration_state)
|
182 |
+
workflow.add_node("SchedulingState", scheduling_state)
|
183 |
+
workflow.add_node("PaymentState", payment_state)
|
184 |
+
workflow.add_node("FinalState", final_state)
|
185 |
+
|
186 |
+
# Define edges
|
187 |
+
workflow.add_edge("InputState", "RegistrationState")
|
188 |
+
workflow.add_edge("RegistrationState", "SchedulingState")
|
189 |
+
workflow.add_edge("SchedulingState", "PaymentState")
|
190 |
+
workflow.add_edge("PaymentState", "FinalState")
|
191 |
+
|
192 |
+
# Entry Point
|
193 |
+
workflow.set_entry_point("InputState")
|
194 |
+
compiled_graph = workflow.compile()
|
195 |
+
|
196 |
+
# ---------------------------------------
|
197 |
+
# Step 6: Gradio Interface
|
198 |
+
# ---------------------------------------
|
199 |
+
def gradio_interface(visitor_name, visitor_mobile, visitor_email, doctor_name, department_name):
|
200 |
+
"""Interface for Gradio application."""
|
201 |
+
state = VisitorState(
|
202 |
+
visitor_name=visitor_name,
|
203 |
+
visitor_mobile=visitor_mobile,
|
204 |
+
visitor_email=visitor_email,
|
205 |
+
doctor_name=doctor_name,
|
206 |
+
department_name=department_name,
|
207 |
+
)
|
208 |
+
# Execute workflow
|
209 |
+
result = compiled_graph.invoke(state.dict())
|
210 |
+
return "\n".join(result["messages"])
|
211 |
+
|
212 |
+
iface = gr.Interface(
|
213 |
+
fn=gradio_interface,
|
214 |
+
inputs=[
|
215 |
+
gr.Textbox(label="Visitor Name"),
|
216 |
+
gr.Textbox(label="Visitor Mobile Number"),
|
217 |
+
gr.Textbox(label="Visitor Email"),
|
218 |
+
gr.Textbox(label="Doctor Name"),
|
219 |
+
gr.Textbox(label="Department Name"),
|
220 |
+
],
|
221 |
+
outputs="textbox",
|
222 |
+
)
|
223 |
+
|
224 |
+
# Execute the Gradio interface
|
225 |
+
if __name__ == "__main__":
|
226 |
+
iface.launch()
|
227 |
+
|
228 |
+
|
229 |
+
"""
|
230 |
import gradio as gr
|
231 |
import spaces
|
232 |
import torch
|
|
|
240 |
return f"Hello {zero + n} Tensor"
|
241 |
|
242 |
demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text())
|
243 |
+
demo.launch()
|
244 |
+
|
245 |
+
"""
|
246 |
+
|