Npps commited on
Commit
6aee7ca
1 Parent(s): 8f8a5f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -25
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import os
2
- import uuid
3
  from langchain_huggingface import HuggingFaceEndpoint
4
  from langchain_core.messages import HumanMessage, SystemMessage
5
  from langchain_core.messages import AIMessage
@@ -9,12 +8,13 @@ from langchain_core.runnables.history import RunnableWithMessageHistory
9
  from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
10
  import gradio as gr
11
 
 
12
  # Set your API keys from environment variables
13
  langchain_key = os.getenv("LANGCHAIN_API_KEY")
14
  HF_key = os.getenv("HUGGINGFACEHUB_TOKEN")
15
- LANGCHAIN_TRACING_V2 = True
16
- LANGCHAIN_ENDPOINT = "https://api.smith.langchain.com"
17
- LANGCHAIN_PROJECT = "LLM_CHATBOT"
18
 
19
  os.environ["LANGCHAIN_TRACING_V2"] = str(LANGCHAIN_TRACING_V2)
20
  os.environ["LANGCHAIN_API_KEY"] = langchain_key
@@ -28,7 +28,7 @@ llm = HuggingFaceEndpoint(
28
  task="text-generation",
29
  max_new_tokens=150,
30
  do_sample=False,
31
- token=HF_key
32
  )
33
 
34
  # Create a Chat Prompt Template
@@ -56,33 +56,17 @@ with_message_history = RunnableWithMessageHistory(chain, get_session_history)
56
  def chat(session_id, user_input):
57
  config = {"configurable": {"session_id": session_id}}
58
  human_message = HumanMessage(content=user_input)
59
- try:
60
- response = with_message_history.invoke({"messages": [human_message]}, config=config)
61
- except Exception as e:
62
- response = AIMessage(content=f"An error occurred: {str(e)}")
63
  return response
64
 
65
- # Gradio session function
66
- def get_session_id(request: gr.Request):
67
- session_id = request.cookies.get("session_id")
68
- if not session_id:
69
- session_id = str(uuid.uuid4())
70
- response = gr.Response().cookies.set("session_id", session_id)
71
- else:
72
- response = gr.Response()
73
- return session_id, response
74
-
75
  # Gradio interface
76
  iface = gr.Interface(
77
  fn=chat,
78
- inputs=[gr.Textbox(lines=1, placeholder="Enter your message")],
79
  outputs="text",
80
  title="LangChain Chatbot",
81
- description="A chatbot that remembers your past interactions. Enter your message."
82
  )
83
 
84
- # Add session management
85
- iface = iface.wrap_inputs(get_session_id, gr.Textbox)
86
-
87
  # Launch the app
88
- iface.launch()
 
1
  import os
 
2
  from langchain_huggingface import HuggingFaceEndpoint
3
  from langchain_core.messages import HumanMessage, SystemMessage
4
  from langchain_core.messages import AIMessage
 
8
  from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
9
  import gradio as gr
10
 
11
+
12
  # Set your API keys from environment variables
13
  langchain_key = os.getenv("LANGCHAIN_API_KEY")
14
  HF_key = os.getenv("HUGGINGFACEHUB_TOKEN")
15
+ LANGCHAIN_TRACING_V2=True
16
+ LANGCHAIN_ENDPOINT="https://api.smith.langchain.com"
17
+ LANGCHAIN_PROJECT="LLM_CHATBOT"
18
 
19
  os.environ["LANGCHAIN_TRACING_V2"] = str(LANGCHAIN_TRACING_V2)
20
  os.environ["LANGCHAIN_API_KEY"] = langchain_key
 
28
  task="text-generation",
29
  max_new_tokens=150,
30
  do_sample=False,
31
+ token =HF_key
32
  )
33
 
34
  # Create a Chat Prompt Template
 
56
  def chat(session_id, user_input):
57
  config = {"configurable": {"session_id": session_id}}
58
  human_message = HumanMessage(content=user_input)
59
+ response = with_message_history.invoke({"messages": [human_message]}, config=config)
 
 
 
60
  return response
61
 
 
 
 
 
 
 
 
 
 
 
62
  # Gradio interface
63
  iface = gr.Interface(
64
  fn=chat,
65
+ inputs=[gr.Textbox(lines=1, placeholder="Enter Session ID"), gr.Textbox(lines=7, placeholder="Enter your message")],
66
  outputs="text",
67
  title="LangChain Chatbot",
68
+ description="A chatbot that remembers your past interactions. Enter your session ID and message."
69
  )
70
 
 
 
 
71
  # Launch the app
72
+ iface.launch()