Bhaskar2611 commited on
Commit
0b60445
·
verified ·
1 Parent(s): 744d8dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -12
app.py CHANGED
@@ -444,13 +444,67 @@
444
  # if __name__ == "__main__":
445
  # demo.launch()
446
 
447
- import os
448
- import subprocess
449
- import gradio as gr
 
 
 
450
 
451
- # Install necessary packages
452
- subprocess.check_call(["pip", "install", "-U", "langchain-openai", "gradio", "langchain-community"])
 
 
 
 
453
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
454
  from langchain_openai import ChatOpenAI
455
  from langchain.prompts import PromptTemplate
456
  from langchain.memory import ConversationBufferMemory
@@ -493,13 +547,13 @@ def get_text_response(user_message, history=None):
493
  # Return the response and updated history
494
  return response['choices'][0]['message']['content'], chat_history
495
 
496
- # Create a Gradio chat interface
497
- demo = gr.Interface(
498
- fn=get_text_response,
499
- inputs=["text", "state"],
500
- outputs=["text", "state"],
501
- )
502
-
503
  if __name__ == "__main__":
504
  demo.launch()
505
 
@@ -511,3 +565,4 @@ if __name__ == "__main__":
511
 
512
 
513
 
 
 
444
  # if __name__ == "__main__":
445
  # demo.launch()
446
 
447
+ # import os
448
+ # import subprocess
449
+ # import gradio as gr
450
+
451
+ # # Install necessary packages
452
+ # subprocess.check_call(["pip", "install", "-U", "langchain-openai", "gradio", "langchain-community"])
453
 
454
+ # from langchain_openai import ChatOpenAI
455
+ # from langchain.prompts import PromptTemplate
456
+ # from langchain.memory import ConversationBufferMemory
457
+
458
+ # # Set OpenAI API Key
459
+ # OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
460
 
461
+ # # Define the template for the chatbot's response
462
+ # template = """You are a helpful assistant to answer all user queries.
463
+ # {chat_history}
464
+ # User: {user_message}
465
+ # Chatbot:"""
466
+
467
+ # # Define the prompt template
468
+ # prompt = PromptTemplate(
469
+ # input_variables=["chat_history", "user_message"],
470
+ # template=template
471
+ # )
472
+
473
+ # # Initialize conversation memory
474
+ # memory = ConversationBufferMemory(memory_key="chat_history")
475
+
476
+ # # Function to get chatbot response
477
+ # def get_text_response(user_message, history=None):
478
+ # # Ensure history is a list
479
+ # if history is None:
480
+ # history = []
481
+
482
+ # # Prepare the conversation history
483
+ # chat_history = history + [f"User: {user_message}"]
484
+
485
+ # # Create the full prompt string
486
+ # full_prompt = prompt.format(chat_history="\n".join(chat_history), user_message=user_message)
487
+
488
+ # llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")
489
+
490
+ # # Use the invoke method instead of __call__
491
+ # response = llm.invoke(full_prompt)
492
+
493
+ # # Return the response and updated history
494
+ # return response['choices'][0]['message']['content'], chat_history
495
+
496
+ # # Create a Gradio chat interface
497
+ # demo = gr.Interface(
498
+ # fn=get_text_response,
499
+ # inputs=["text", "state"],
500
+ # outputs=["text", "state"],
501
+ # )
502
+
503
+ # if __name__ == "__main__":
504
+ # demo.launch()
505
+
506
+ import os
507
+ import gradio as gr
508
  from langchain_openai import ChatOpenAI
509
  from langchain.prompts import PromptTemplate
510
  from langchain.memory import ConversationBufferMemory
 
547
  # Return the response and updated history
548
  return response['choices'][0]['message']['content'], chat_history
549
 
550
+ # Create a Gradio chat interface using ChatInterface
551
+ with gr.Blocks() as demo:
552
+ chatbot = gr.ChatInterface(
553
+ get_text_response, # Function to get responses
554
+ memory=ConversationBufferMemory() # Gradio stateful chat memory
555
+ )
556
+
557
  if __name__ == "__main__":
558
  demo.launch()
559
 
 
565
 
566
 
567
 
568
+