Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 3 |
+
from langchain_core.messages import HumanMessage, SystemMessage
|
| 4 |
+
from langchain_core.messages import AIMessage
|
| 5 |
+
from langchain_community.chat_message_histories import ChatMessageHistory
|
| 6 |
+
from langchain_core.chat_history import BaseChatMessageHistory
|
| 7 |
+
from langchain_core.runnables.history import RunnableWithMessageHistory
|
| 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
|
| 21 |
+
os.environ["HUGGINGFACEHUB_TOKEN"] = HF_key
|
| 22 |
+
os.environ["LANGCHAIN_ENDPOINT"] = LANGCHAIN_ENDPOINT
|
| 23 |
+
os.environ["LANGCHAIN_PROJECT"] = LANGCHAIN_PROJECT
|
| 24 |
+
|
| 25 |
+
# Initialize the Chat Model
|
| 26 |
+
llm = HuggingFaceEndpoint(
|
| 27 |
+
repo_id="microsoft/Phi-3-vision-128k-instruct",
|
| 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
|
| 35 |
+
prompt = ChatPromptTemplate.from_messages(
|
| 36 |
+
[
|
| 37 |
+
("system", "You are a helpful assistant. Answer all questions to the best of your ability."),
|
| 38 |
+
MessagesPlaceholder(variable_name="messages"),
|
| 39 |
+
]
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Set up the chain
|
| 43 |
+
chain = prompt | llm
|
| 44 |
+
|
| 45 |
+
# Set up message history
|
| 46 |
+
store = {}
|
| 47 |
+
|
| 48 |
+
def get_session_history(session_id: str) -> BaseChatMessageHistory:
|
| 49 |
+
if session_id not in store:
|
| 50 |
+
store[session_id] = ChatMessageHistory()
|
| 51 |
+
return store[session_id]
|
| 52 |
+
|
| 53 |
+
with_message_history = RunnableWithMessageHistory(chain, get_session_history)
|
| 54 |
+
|
| 55 |
+
# Gradio chat function
|
| 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()
|
| 73 |
+
|