init
Browse files- README.md +10 -1
- chat_app.py +115 -0
README.md
CHANGED
|
@@ -1 +1,10 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: HF Chatbot
|
| 3 |
+
emoji: 🤗
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: gray
|
| 6 |
+
sdk: gradio
|
| 7 |
+
app_file: chat_app.py
|
| 8 |
+
pinned: false
|
| 9 |
+
---
|
| 10 |
+
# hf-olmocr-demo
|
chat_app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from typing import List
|
| 5 |
+
|
| 6 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 7 |
+
|
| 8 |
+
from langchain.schema import BaseMessage
|
| 9 |
+
from langchain_core.chat_history import BaseChatMessageHistory
|
| 10 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 11 |
+
from langchain_core.runnables import (
|
| 12 |
+
ConfigurableFieldSpec,
|
| 13 |
+
)
|
| 14 |
+
from langchain_core.runnables.history import RunnableWithMessageHistory
|
| 15 |
+
|
| 16 |
+
from pydantic import BaseModel, Field
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class InMemoryHistory(BaseChatMessageHistory, BaseModel):
|
| 20 |
+
"""In memory implementation of chat message history."""
|
| 21 |
+
|
| 22 |
+
messages: List[BaseMessage] = Field(default_factory=list)
|
| 23 |
+
|
| 24 |
+
def add_messages(self, messages: List[BaseMessage]) -> None:
|
| 25 |
+
"""Add a list of messages to the store"""
|
| 26 |
+
self.messages.extend(messages)
|
| 27 |
+
|
| 28 |
+
def clear(self) -> None:
|
| 29 |
+
self.messages = []
|
| 30 |
+
|
| 31 |
+
# In-memory storage for session history
|
| 32 |
+
store = {}
|
| 33 |
+
|
| 34 |
+
def get_session_history(
|
| 35 |
+
user_id: str, conversation_id: str
|
| 36 |
+
) -> BaseChatMessageHistory:
|
| 37 |
+
if (user_id, conversation_id) not in store:
|
| 38 |
+
store[(user_id, conversation_id)] = InMemoryHistory()
|
| 39 |
+
return store[(user_id, conversation_id)]
|
| 40 |
+
|
| 41 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 42 |
+
("system", "You're an assistant who's good at everything"),
|
| 43 |
+
MessagesPlaceholder(variable_name="history"),
|
| 44 |
+
("human", "{question}"),
|
| 45 |
+
])
|
| 46 |
+
|
| 47 |
+
model_id="mistralai/Mistral-7B-Instruct-v0.3"
|
| 48 |
+
llm = HuggingFaceEndpoint(
|
| 49 |
+
repo_id=model_id,
|
| 50 |
+
max_new_tokens=1024,
|
| 51 |
+
temperature=0.1,
|
| 52 |
+
huggingfacehub_api_token=os.environ.get("my",""),
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
chain = prompt | llm
|
| 56 |
+
|
| 57 |
+
with_message_history = RunnableWithMessageHistory(
|
| 58 |
+
chain,
|
| 59 |
+
get_session_history=get_session_history,
|
| 60 |
+
input_messages_key="question",
|
| 61 |
+
history_messages_key="history",
|
| 62 |
+
history_factory_config=[
|
| 63 |
+
ConfigurableFieldSpec(
|
| 64 |
+
id="user_id",
|
| 65 |
+
annotation=str,
|
| 66 |
+
name="User ID",
|
| 67 |
+
description="Unique identifier for the user.",
|
| 68 |
+
default="",
|
| 69 |
+
is_shared=True,
|
| 70 |
+
),
|
| 71 |
+
ConfigurableFieldSpec(
|
| 72 |
+
id="conversation_id",
|
| 73 |
+
annotation=str,
|
| 74 |
+
name="Conversation ID",
|
| 75 |
+
description="Unique identifier for the conversation.",
|
| 76 |
+
default="",
|
| 77 |
+
is_shared=True,
|
| 78 |
+
),
|
| 79 |
+
],
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
with gr.Blocks() as demo:
|
| 83 |
+
chatbot = gr.Chatbot(type="messages")
|
| 84 |
+
msg = gr.Textbox()
|
| 85 |
+
stop = gr.Button("Stop")
|
| 86 |
+
clear = gr.Button("Clear")
|
| 87 |
+
|
| 88 |
+
def user(user_message, history: list):
|
| 89 |
+
return "", history + [{"role": "user", "content": user_message}]
|
| 90 |
+
|
| 91 |
+
def bot(history: list):
|
| 92 |
+
|
| 93 |
+
question = history[-1]['content']
|
| 94 |
+
|
| 95 |
+
answer = with_message_history.stream(
|
| 96 |
+
{"ability": "everything", "question": question},
|
| 97 |
+
config={"configurable": {"user_id": "123", "conversation_id": "1"}}
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
history.append({"role": "assistant", "content": ""})
|
| 101 |
+
for item in answer:
|
| 102 |
+
for character in item.content:
|
| 103 |
+
history[-1]['content'] += character
|
| 104 |
+
time.sleep(0.05)
|
| 105 |
+
yield history
|
| 106 |
+
|
| 107 |
+
submit_event = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=True).then(
|
| 108 |
+
bot, chatbot, chatbot
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
stop.click(None, None, None, cancels=[submit_event], queue=False)
|
| 112 |
+
clear.click(lambda: None, None, chatbot, queue=True)
|
| 113 |
+
|
| 114 |
+
if __name__ == "__main__":
|
| 115 |
+
demo.launch()
|