Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
| 3 |
+
from langchain.chains import LLMChain
|
| 4 |
+
from langchain_core.prompts import PromptTemplate
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from langchain_community.chat_message_histories import ChatMessageHistory
|
| 7 |
+
from langchain_core.chat_history import BaseChatMessageHistory
|
| 8 |
+
from langchain_core.runnables.history import RunnableWithMessageHistory
|
| 9 |
+
from langchain_core.messages import HumanMessage
|
| 10 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 14 |
+
llm = HuggingFaceEndpoint(
|
| 15 |
+
repo_id=repo_id, max_length=128, temperature=0.2
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
store = {}
|
| 19 |
+
|
| 20 |
+
def llm_chain(question, chat_history):
|
| 21 |
+
|
| 22 |
+
def get_session_history(session_id: str) -> BaseChatMessageHistory:
|
| 23 |
+
if session_id not in store:
|
| 24 |
+
store[session_id] = ChatMessageHistory()
|
| 25 |
+
return store[session_id]
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
template = """Question: {question}
|
| 30 |
+
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
prompt = PromptTemplate.from_template(template)
|
| 34 |
+
|
| 35 |
+
chain = prompt | llm
|
| 36 |
+
|
| 37 |
+
with_message_history = RunnableWithMessageHistory(chain, get_session_history)
|
| 38 |
+
config = {"configurable": {"session_id": "abc1"}}
|
| 39 |
+
response = with_message_history.invoke(
|
| 40 |
+
[HumanMessage(content=question)],
|
| 41 |
+
config=config,
|
| 42 |
+
)
|
| 43 |
+
chat_history.append((question, response))
|
| 44 |
+
|
| 45 |
+
return "", chat_history
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
with gr.Row():
|
| 51 |
+
with gr.Column():
|
| 52 |
+
chatbot = gr.Chatbot()
|
| 53 |
+
msg = gr.Textbox(interactive=True, )
|
| 54 |
+
with gr.Row():
|
| 55 |
+
clear = gr.ClearButton([msg, chatbot], icon="https://img.icons8.com/?size=100&id=Xnx8cxDef16O&format=png&color=000000")
|
| 56 |
+
send_btn = gr.Button("Send", variant='primary', icon="https://img.icons8.com/?size=100&id=g8ltXTwIfJ1n&format=png&color=000000")
|
| 57 |
+
msg.submit(fn=llm_chain, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 58 |
+
send_btn.click(fn=llm_chain, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
demo.launch()
|