Sreekan commited on
Commit
cc5d590
·
verified ·
1 Parent(s): e506aeb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
@@ -62,3 +63,71 @@ demo = gr.ChatInterface(
62
 
63
  if __name__ == "__main__":
64
  demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
 
 
63
 
64
  if __name__ == "__main__":
65
  demo.launch()
66
+ '''
67
+
68
+ import gradio as gr
69
+ from langchain.chains import LLMChain
70
+ from langchain.prompts import PromptTemplate
71
+ from langchain_huggingface import HuggingFaceEndpoint
72
+ from langgraph.graph import StateGraph
73
+
74
+ # Define the LLM models
75
+ llm1 = HuggingFaceEndpoint(model='t5-small')
76
+ llm2 = HuggingFaceEndpoint(model='t5-large')
77
+
78
+ # Define the agent functions
79
+ def agent1(query):
80
+ return f"Agent 1: {query}"
81
+
82
+ def agent2(query):
83
+ return f"Agent 2: {query}"
84
+
85
+ # Define the states
86
+ s1 = StateGraph("s1")
87
+ s2 = StateGraph("s2")
88
+
89
+ # Define the LLM chains
90
+ chain1 = LLMChain(llm=llm1, prompt=PromptTemplate(input_variables=["query"], template="You are in state s1. {{query}}"))
91
+ chain2 = LLMChain(llm=llm2, prompt=PromptTemplate(input_variables=["query"], template="You are in state s2. {{query}}"))
92
+
93
+ # Define the transition functions
94
+ def transition_s1(query):
95
+ output = chain1.invoke(query=query)
96
+ return agent1(output), s2
97
+
98
+ def transition_s2(query):
99
+ output = chain2.invoke(query=query)
100
+ return agent2(output), s1
101
+
102
+ # Define the respond function
103
+ def respond(input, history, current_state):
104
+ if current_state == s1:
105
+ response, next_state = transition_s1(input)
106
+ elif current_state == s2:
107
+ response, next_state = transition_s2(input)
108
+ history.append((input, response))
109
+ return history, next_state
110
+
111
+ # Create the Gradio interface
112
+ current_state = s1 # Define current_state here
113
+
114
+ with gr.Blocks() as demo:
115
+ gr.Markdown("# Chatbot Interface")
116
+ chatbot_interface = gr.Chatbot()
117
+ user_input = gr.Textbox(label="Your Message", placeholder="Type something...")
118
+ submit_btn = gr.Button("Send")
119
+
120
+ # Define the behavior of the submit button
121
+ def submit_click(input, history):
122
+ global current_state # Use global instead of nonlocal
123
+ history, current_state = respond(input, history, current_state)
124
+ return history
125
+
126
+ submit_btn.click(
127
+ fn=submit_click,
128
+ inputs=[user_input, chatbot_interface],
129
+ outputs=chatbot_interface
130
+ )
131
+
132
+ # Launch the Gradio application
133
+ demo.launch()