jsmidt commited on
Commit
7422c70
·
1 Parent(s): 6d1e077

Initial Commit

Browse files
Files changed (2) hide show
  1. app.py +113 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
3
+ from langchain_core.messages import trim_messages
4
+ from langgraph.graph import START, MessagesState, StateGraph
5
+ from langgraph.checkpoint.memory import MemorySaver
6
+ from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
7
+ from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
8
+
9
+
10
+
11
+ llm = HuggingFaceEndpoint(
12
+ repo_id="meta-llama/Llama-3.2-3B-Instruct",
13
+ max_new_tokens=512,
14
+ temperature=0.7,
15
+ )
16
+ model = ChatHuggingFace(llm=llm)
17
+
18
+ # Define message trimmer
19
+ trimmer = trim_messages(
20
+ max_tokens=8192,
21
+ strategy="last",
22
+ token_counter=model,
23
+ include_system=True,
24
+ allow_partial=False,
25
+ start_on="human",
26
+ )
27
+
28
+
29
+ # Define the workflow
30
+ workflow = StateGraph(state_schema=MessagesState)
31
+ app = workflow.compile(checkpointer=MemorySaver())
32
+
33
+ config = {"configurable": {"thread_id": "abc123"}}
34
+
35
+
36
+ # Function to handle chat interaction
37
+ def chat_fn(system_prompt, user_input, history):
38
+ if history is None:
39
+ history = []
40
+
41
+ # Append user input
42
+ history.append((user_input, ""))
43
+
44
+ # Build messages for the model
45
+ messages = [SystemMessage(system_prompt)]
46
+ for user_msg, assistant_msg in history[:-1]:
47
+ messages.append(HumanMessage(user_msg))
48
+ messages.append(AIMessage(assistant_msg))
49
+ messages.append(HumanMessage(user_input))
50
+
51
+ # Trim messages
52
+ trimmed_messages = trimmer.invoke(messages)
53
+
54
+ # Create prompt template with current system prompt
55
+ prompt_template = ChatPromptTemplate.from_messages(
56
+ [
57
+ ("system", system_prompt),
58
+ MessagesPlaceholder(variable_name="messages"),
59
+ ]
60
+ )
61
+
62
+ # Prepare the prompt
63
+ prompt = prompt_template.invoke(trimmed_messages)
64
+
65
+ # Call the model
66
+ response = model.invoke(prompt)
67
+
68
+ # Get assistant's reply
69
+ print(response)
70
+ # assistant_reply = response["messages"][-1].content
71
+ assistant_reply = response.content
72
+
73
+ # Update history with assistant's reply
74
+ history[-1] = (user_input, assistant_reply)
75
+
76
+ return history, history
77
+
78
+
79
+ # Build Gradio interface
80
+ with gr.Blocks() as demo:
81
+ system_prompt = gr.Textbox(
82
+ value="You talk like a pirate. Answer all questions to the best of your ability.",
83
+ label="System Prompt",
84
+ lines=2,
85
+ )
86
+ chatbot = gr.Chatbot()
87
+ state = gr.State()
88
+
89
+ with gr.Row():
90
+ user_input = gr.Textbox(
91
+ show_label=False,
92
+ placeholder="Enter your message",
93
+ container=False, # Moved 'container' parameter here
94
+ )
95
+ send_button = gr.Button("Send")
96
+
97
+ # Define interaction
98
+ def user_message(_):
99
+ return "", ""
100
+
101
+ send_button.click(
102
+ fn=chat_fn,
103
+ inputs=[system_prompt, user_input, state],
104
+ outputs=[chatbot, state],
105
+ )
106
+ user_input.submit(
107
+ fn=chat_fn,
108
+ inputs=[system_prompt, user_input, state],
109
+ outputs=[chatbot, state],
110
+ )
111
+
112
+ if __name__ == '__main__':
113
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ langchain_core
3
+ langgraph
4
+ langchain_core
5
+ langchain_huggingface