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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0318caf
d5a8216
 
d35c879
b307974
 
d35c879
d5a8216
 
 
 
 
 
 
 
 
 
 
 
dda8f2a
 
417a23b
d5a8216
d141979
 
 
 
 
417a23b
dda8f2a
744d8dc
 
 
 
25606a9
744d8dc
 
 
8f6ec4b
 
 
d35c879
0b60445
 
 
 
 
 
 
d35c879
b307974
0318caf
4b46c96
417a23b
0d53d23
dda8f2a
d141979
25606a9
8f6ec4b
744d8dc
0b60445
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# import os
# import gradio as gr
# from langchain.chat_models import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.chains import LLMChain
# from langchain.memory import ConversationBufferMemory

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory
# memory = ConversationBufferMemory(memory_key="chat_history")

# # Define the LLM chain with the ChatOpenAI model and conversation memory
# llm_chain = LLMChain(
#     llm=ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo"),  # Use 'model' instead of 'model_name'
#     prompt=prompt,
#     verbose=True,
#     memory=memory,
# )

# # Function to get chatbot response
# def get_text_response(user_message, history):
#     response = llm_chain.predict(user_message=user_message)
#     return response

# # Create a Gradio chat interface
# demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")

# if __name__ == "__main__":
#     demo.launch()

# import subprocess

# # Run the Bash script that installs dependencies and runs the app
# subprocess.run(['./run.sh'])

# # Rest of your application code can go here
# import subprocess
# import os

# # Ensure the run.sh script has executable permissions
# # subprocess.run(['chmod', '+x', './run.sh'])

# # Run the Bash script that installs dependencies and runs the app
# # subprocess.run(['./run.sh'])

# import gradio as gr
# from langchain_openai import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.memory import ConversationBufferMemory
# from langchain.chains import RunnableSequence

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory
# memory = ConversationBufferMemory(memory_key="chat_history")

# # Define the LLM (language model)
# llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")

# # Define the chain using RunnableSequence (replace LLMChain)
# llm_chain = prompt | llm  # Chaining the prompt and the LLM

# # Function to get chatbot response
# def get_text_response(user_message, history):
#     inputs = {"chat_history": history, "user_message": user_message}
#     response = llm_chain(inputs)
#     return response['text']

# # Create a Gradio chat interface
# demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")

# if __name__ == "__main__":
#     demo.launch()


# import os
# import gradio as gr
# from langchain_openai import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.memory import ConversationBufferMemory
# from langchain.chains import LLMChain

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory
# memory = ConversationBufferMemory(memory_key="chat_history")

# # Define the LLM (language model) and chain
# llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")
# llm_chain = LLMChain(
#     llm=llm,
#     prompt=prompt,
#     verbose=True,
#     memory=memory,
# )

# # Function to get chatbot response
# def get_text_response(user_message, history):
#     response = llm_chain.predict(user_message=user_message)
#     return response

# # Create a Gradio chat interface
# demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")

# if __name__ == "__main__":
#     demo.launch()

# import os
# import gradio as gr
# from langchain_openai import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.memory import ConversationBufferMemory
# from langchain.schema import AIMessage, HumanMessage
# from langchain.chains import RunnableSequence

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory (following migration guide)
# memory = ConversationBufferMemory(return_messages=True)  # Use return_messages=True for updated usage

# # Define the LLM (language model)
# llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")

# # Create the RunnableSequence instead of LLMChain
# llm_sequence = prompt | llm  # This pipelines the prompt into the language model

# # Function to get chatbot response
# def get_text_response(user_message, history):
#     # Prepare the conversation history
#     chat_history = [HumanMessage(content=user_message)]
    
#     # Pass the prompt and history to the language model sequence
#     response = llm_sequence.invoke({"chat_history": history, "user_message": user_message})
    
#     return response

# # Create a Gradio chat interface
# demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")

# if __name__ == "__main__":
#     demo.launch()

# import os
# import gradio as gr
# from langchain_openai import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.memory import ConversationBufferMemory
# from langchain.schema import AIMessage, HumanMessage
# from langchain import Runnable  # Using Runnable instead of RunnableSequence

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory (following migration guide)
# memory = ConversationBufferMemory(return_messages=True)  # Use return_messages=True for updated usage

# # Define the LLM (language model)
# llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")

# # Create the Runnable instead of RunnableSequence
# llm_runnable = Runnable(lambda inputs: prompt.format(**inputs)) | llm

# # Function to get chatbot response
# def get_text_response(user_message, history):
#     # Prepare the conversation history
#     chat_history = [HumanMessage(content=user_message)]
    
#     # Pass the prompt and history to the language model sequence
#     response = llm_runnable.invoke({"chat_history": history, "user_message": user_message})
    
#     return response

# # Create a Gradio chat interface
# demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")

# if __name__ == "__main__":
#     demo.launch()

# import os
# import subprocess
# import gradio as gr

# # Install necessary packages
# subprocess.check_call(["pip", "install", "-U", "langchain-openai", "gradio", "langchain-community"])

# from langchain_openai import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.chains import LLMChain
# from langchain.memory import ConversationBufferMemory

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory
# memory = ConversationBufferMemory(memory_key="chat_history")

# # Define the LLM chain with the ChatOpenAI model and conversation memory
# llm_chain = LLMChain(
#     llm=ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo"),  # Use 'model' instead of 'model_name'
#     prompt=prompt,
#     verbose=True,
#     memory=memory,
# )

# # Function to get chatbot response
# def get_text_response(user_message, history):
#     # Prepare the conversation history
#     chat_history = history + [f"User: {user_message}"]
#     response = llm_chain.predict(user_message=user_message, chat_history=chat_history)
#     return response

# # Create a Gradio chat interface
# demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")

# if __name__ == "__main__":
#     demo.launch()

# import os
# import subprocess
# import gradio as gr

# # Install necessary packages
# subprocess.check_call(["pip", "install", "-U", "langchain-openai", "gradio", "langchain-community"])

# from langchain_openai import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.memory import ConversationBufferMemory
# from langchain.chains import Runnable, RunnableSequence

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory
# memory = ConversationBufferMemory(memory_key="chat_history")

# # Define the runnable sequence
# chatbot_runnable = RunnableSequence(prompt | ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo"))

# # Function to get chatbot response
# def get_text_response(user_message, history=None):
#     # Ensure history is a list
#     if history is None:
#         history = []

#     # Prepare the conversation history
#     chat_history = history + [f"User: {user_message}"]
#     response = chatbot_runnable.invoke({"chat_history": "\n".join(chat_history), "user_message": user_message})
    
#     return response

# # Create a Gradio chat interface
# demo = gr.Interface(fn=get_text_response, inputs=["text", "state"], outputs="text")

# if __name__ == "__main__":
#     demo.launch()

# import os
# import subprocess
# import gradio as gr

# # Install necessary packages
# subprocess.check_call(["pip", "install", "-U", "langchain-openai", "gradio", "langchain-community"])

# from langchain_openai import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.memory import ConversationBufferMemory

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory
# memory = ConversationBufferMemory(memory_key="chat_history")

# # Function to get chatbot response
# def get_text_response(user_message, history=None):
#     # Ensure history is a list
#     if history is None:
#         history = []

#     # Prepare the conversation history
#     chat_history = history + [f"User: {user_message}"]
#     llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")
#     response = llm({"chat_history": "\n".join(chat_history), "user_message": user_message})
    
#     return response['choices'][0]['message']['content']

# # Create a Gradio chat interface
# demo = gr.Interface(fn=get_text_response, inputs=["text", "state"], outputs="text")

# if __name__ == "__main__":
#     demo.launch()

# import os
# import subprocess
# import gradio as gr

# # Install necessary packages
# subprocess.check_call(["pip", "install", "-U", "langchain-openai", "gradio", "langchain-community"])

# from langchain_openai import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.memory import ConversationBufferMemory

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory
# memory = ConversationBufferMemory(memory_key="chat_history")

# # Function to get chatbot response
# def get_text_response(user_message, history=None):
#     # Ensure history is a list
#     if history is None:
#         history = []

#     # Prepare the conversation history
#     chat_history = history + [f"User: {user_message}"]
#     llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")
#     response = llm({"chat_history": "\n".join(chat_history), "user_message": user_message})

#     # Return the response and updated history
#     return response['choices'][0]['message']['content'], chat_history

# # Create a Gradio chat interface
# demo = gr.Interface(
#     fn=get_text_response, 
#     inputs=["text", "state"], 
#     outputs=["text", "state"],
# )

# if __name__ == "__main__":
#     demo.launch()

# import os
# import subprocess
# import gradio as gr

# # Install necessary packages
# subprocess.check_call(["pip", "install", "-U", "langchain-openai", "gradio", "langchain-community"])

# from langchain_openai import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.memory import ConversationBufferMemory

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory
# memory = ConversationBufferMemory(memory_key="chat_history")

# # Function to get chatbot response
# def get_text_response(user_message, history=None):
#     # Ensure history is a list
#     if history is None:
#         history = []

#     # Prepare the conversation history
#     chat_history = history + [f"User: {user_message}"]
    
#     # Create the full prompt string
#     full_prompt = prompt.format(chat_history="\n".join(chat_history), user_message=user_message)

#     llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")
    
#     # Use the invoke method instead of __call__
#     response = llm.invoke(full_prompt)

#     # Return the response and updated history
#     return response['choices'][0]['message']['content'], chat_history

# # Create a Gradio chat interface
# demo = gr.Interface(
#     fn=get_text_response, 
#     inputs=["text", "state"], 
#     outputs=["text", "state"],
# )

# if __name__ == "__main__":
#     demo.launch()

import os
import gradio as gr
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.memory import ConversationBufferMemory

# Set OpenAI API Key
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# Define the template for the chatbot's response
template = """You are a helpful assistant to answer all user queries.
{chat_history}
User: {user_message}
Chatbot:"""

# Define the prompt template
prompt = PromptTemplate(
    input_variables=["chat_history", "user_message"], 
    template=template
)

# Initialize conversation memory
memory = ConversationBufferMemory(memory_key="chat_history")

# Function to get chatbot response
def get_text_response(user_message, history=None):
    # Ensure history is a list
    if history is None:
        history = []

    # Prepare the conversation history
    chat_history = history + [f"User: {user_message}"]
    
    # Create the full prompt string
    full_prompt = prompt.format(chat_history="\n".join(chat_history), user_message=user_message)

    llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")
    
    # Use the invoke method instead of __call__
    response = llm.invoke(full_prompt)

    # Return the response and updated history
    return response['choices'][0]['message']['content'], chat_history

# Create a Gradio chat interface using ChatInterface
with gr.Blocks() as demo:
    chatbot = gr.ChatInterface(
        get_text_response,  # Function to get responses
        memory=ConversationBufferMemory()  # Gradio stateful chat memory
    )
    
if __name__ == "__main__":
    demo.launch()