Mykes commited on
Commit
6d70f5d
β€’
1 Parent(s): 2d044a5

Upload app_interface_working_1.py

Browse files
Files changed (1) hide show
  1. app_interface_working_1.py +105 -0
app_interface_working_1.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from llama_cpp import Llama
3
+
4
+ st.set_page_config(page_title="Chat with AI", page_icon="πŸ€–")
5
+
6
+ # Custom CSS for better styling
7
+ st.markdown("""
8
+ <style>
9
+ .stTextInput > div > div > input {
10
+ background-color: #f0f2f6;
11
+ }
12
+ .chat-message {
13
+ padding: 1.5rem; border-radius: 0.5rem; margin-bottom: 1rem; display: flex
14
+ }
15
+ .chat-message.user {
16
+ background-color: #2b313e
17
+ }
18
+ .chat-message.bot {
19
+ background-color: #475063
20
+ }
21
+ .chat-message .avatar {
22
+ width: 20%;
23
+ }
24
+ .chat-message .avatar img {
25
+ max-width: 78px;
26
+ max-height: 78px;
27
+ border-radius: 50%;
28
+ object-fit: cover;
29
+ }
30
+ .chat-message .message {
31
+ width: 80%;
32
+ padding: 0 1.5rem;
33
+ color: #fff;
34
+ }
35
+ </style>
36
+ """, unsafe_allow_html=True)
37
+
38
+ @st.cache_resource
39
+ def load_model():
40
+ return Llama.from_pretrained(
41
+ repo_id="Mykes/med_phi3-mini-4k-GGUF",
42
+ filename="*Q4_K_M.gguf",
43
+ verbose=False,
44
+ n_ctx=512,
45
+ n_batch=256,
46
+ n_threads=4
47
+ )
48
+
49
+ llm = load_model()
50
+
51
+ def format_context(messages):
52
+ context = ""
53
+ for message in messages:
54
+ if message["role"] == "user":
55
+ context += f"Human: {message['content']}\n"
56
+ else:
57
+ context += f"Assistant: {message['content']}\n"
58
+ return context
59
+
60
+ # Initialize chat history
61
+ if "messages" not in st.session_state:
62
+ st.session_state.messages = []
63
+
64
+ # Display chat messages from history on app rerun
65
+ for message in st.session_state.messages:
66
+ with st.chat_message(message["role"]):
67
+ st.markdown(message["content"])
68
+
69
+ # React to user input
70
+ if prompt := st.chat_input("What is your question?"):
71
+ # Display user message in chat message container
72
+ st.chat_message("user").markdown(prompt)
73
+ # Add user message to chat history
74
+ st.session_state.messages.append({"role": "user", "content": prompt})
75
+
76
+ # Format the context with the last 5 messages
77
+ context = format_context(st.session_state.messages[-5:])
78
+
79
+ # Prepare the model input
80
+ model_input = f"{context}Human: {prompt}\nAssistant:"
81
+
82
+ # Display assistant response in chat message container
83
+ with st.chat_message("assistant"):
84
+ message_placeholder = st.empty()
85
+ full_response = ""
86
+
87
+ for token in llm(
88
+ model_input,
89
+ max_tokens=None,
90
+ stop=["Human:", "<end_of_turn>"],
91
+ echo=True,
92
+ stream=True
93
+ ):
94
+ full_response += token['choices'][0]['text']
95
+ message_placeholder.markdown(full_response + "β–Œ")
96
+
97
+ # Remove the initial context and prompt from the response
98
+ assistant_response = full_response.split("Assistant:")[-1].strip()
99
+ message_placeholder.markdown(assistant_response)
100
+
101
+ # Add assistant response to chat history
102
+ st.session_state.messages.append({"role": "assistant", "content": assistant_response})
103
+
104
+ st.sidebar.title("Chat with AI")
105
+ st.sidebar.markdown("This is a simple chat interface using Streamlit and an AI model.")