Spaces:
Sleeping
Sleeping
Nathan Butters
commited on
Commit
·
3a0c83a
1
Parent(s):
46f839b
Fix chat output
Browse files- .gitignore +1 -0
- app.py +2 -2
- helpers/chat.py +58 -41
.gitignore
CHANGED
|
@@ -164,3 +164,4 @@ cython_debug/
|
|
| 164 |
|
| 165 |
# Other
|
| 166 |
tempDir/picture.png
|
|
|
|
|
|
| 164 |
|
| 165 |
# Other
|
| 166 |
tempDir/picture.png
|
| 167 |
+
|
app.py
CHANGED
|
@@ -51,7 +51,7 @@ if picture is not None:
|
|
| 51 |
with open(os.path.join("tempDir","picture.png"),"wb") as f:
|
| 52 |
f.write(picture.getbuffer())
|
| 53 |
img_url = f"https://huggingface.co/spaces/butterswords/MM_Math_Helper/resolve/main/tempDir/picture.png"
|
| 54 |
-
mmChat(img_url)
|
| 55 |
-
|
| 56 |
else:
|
| 57 |
basicChat()
|
|
|
|
| 51 |
with open(os.path.join("tempDir","picture.png"),"wb") as f:
|
| 52 |
f.write(picture.getbuffer())
|
| 53 |
img_url = f"https://huggingface.co/spaces/butterswords/MM_Math_Helper/resolve/main/tempDir/picture.png"
|
| 54 |
+
#mmChat(img_url)
|
| 55 |
+
guidedMM(st.session_state.systemPrompt, img_url)
|
| 56 |
else:
|
| 57 |
basicChat()
|
helpers/chat.py
CHANGED
|
@@ -17,31 +17,65 @@ def hf_stream(model_name: str, messages: dict):
|
|
| 17 |
for chunk in stream:
|
| 18 |
chunk.choices[0].delta.content, end=""
|
| 19 |
|
| 20 |
-
def hf_generator(model,prompt,data):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
{
|
| 30 |
-
"type": "image_url",
|
| 31 |
-
"image_url": {
|
| 32 |
-
"url": data
|
| 33 |
}
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
completion = client.chat.completions.create(
|
| 40 |
model=model,
|
| 41 |
messages=messages,
|
| 42 |
max_tokens=500
|
| 43 |
)
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
def basicChat():
|
| 47 |
# Accept user input and then writes the response
|
|
@@ -80,7 +114,7 @@ def mmChat(data):
|
|
| 80 |
|
| 81 |
with st.chat_message(st.session_state.model):
|
| 82 |
logger.info(f"Message to {st.session_state.model}: {st.session_state.messages[-1]}")
|
| 83 |
-
response = st.
|
| 84 |
st.session_state.model,
|
| 85 |
prompt,
|
| 86 |
data))
|
|
@@ -96,34 +130,17 @@ def guidedMM(sysChoice:str, data):
|
|
| 96 |
|
| 97 |
if prompt := st.chat_input("How may I help you learn math today?"):
|
| 98 |
# Add user message to chat history
|
| 99 |
-
st.session_state.messages.append([
|
| 100 |
-
{
|
| 101 |
-
"role": "user",
|
| 102 |
-
"content": [
|
| 103 |
-
{
|
| 104 |
-
"type": "text",
|
| 105 |
-
"text": prompt
|
| 106 |
-
},
|
| 107 |
-
{
|
| 108 |
-
"type": "image_url",
|
| 109 |
-
"image_url": {
|
| 110 |
-
"url": data
|
| 111 |
-
}
|
| 112 |
-
}
|
| 113 |
-
]
|
| 114 |
-
}
|
| 115 |
-
])
|
| 116 |
-
logger.info(st.session_state.messages[-2:])
|
| 117 |
# Display user message in chat message container
|
| 118 |
with st.chat_message("user"):
|
| 119 |
st.markdown(prompt)
|
| 120 |
|
| 121 |
with st.chat_message(st.session_state.model):
|
| 122 |
logger.info(f"Message to {st.session_state.model}: {st.session_state.messages[-1]}")
|
| 123 |
-
response = st.
|
| 124 |
st.session_state.model,
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
))
|
| 127 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 128 |
-
logger.info(st.session_state.messages[-1])
|
| 129 |
|
|
|
|
| 17 |
for chunk in stream:
|
| 18 |
chunk.choices[0].delta.content, end=""
|
| 19 |
|
| 20 |
+
def hf_generator(model,prompt,data,system=None):
|
| 21 |
+
if system:
|
| 22 |
+
messages = [
|
| 23 |
+
{
|
| 24 |
+
"role": "system",
|
| 25 |
+
"content": [
|
| 26 |
+
{
|
| 27 |
+
"type": "text",
|
| 28 |
+
"text": system
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
+
]
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"role": "user",
|
| 34 |
+
"content": [
|
| 35 |
+
{
|
| 36 |
+
"type": "text",
|
| 37 |
+
"text": prompt
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"type": "image_url",
|
| 41 |
+
"image_url": {
|
| 42 |
+
"url": data
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
]
|
| 46 |
+
}
|
| 47 |
+
]
|
| 48 |
+
else:
|
| 49 |
+
messages = [
|
| 50 |
+
{
|
| 51 |
+
"role": "user",
|
| 52 |
+
"content": [
|
| 53 |
+
{
|
| 54 |
+
"type": "text",
|
| 55 |
+
"text": prompt
|
| 56 |
+
},
|
| 57 |
+
{
|
| 58 |
+
"type": "image_url",
|
| 59 |
+
"image_url": {
|
| 60 |
+
"url": data
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
]
|
| 64 |
+
}
|
| 65 |
+
]
|
| 66 |
|
| 67 |
completion = client.chat.completions.create(
|
| 68 |
model=model,
|
| 69 |
messages=messages,
|
| 70 |
max_tokens=500
|
| 71 |
)
|
| 72 |
+
response = completion.choices[0].message.content
|
| 73 |
+
logger.info({"role": "assistant", "content": response})
|
| 74 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 75 |
+
return completion.choices[0].message.content
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
|
| 80 |
def basicChat():
|
| 81 |
# Accept user input and then writes the response
|
|
|
|
| 114 |
|
| 115 |
with st.chat_message(st.session_state.model):
|
| 116 |
logger.info(f"Message to {st.session_state.model}: {st.session_state.messages[-1]}")
|
| 117 |
+
response = st.write(hf_generator(
|
| 118 |
st.session_state.model,
|
| 119 |
prompt,
|
| 120 |
data))
|
|
|
|
| 130 |
|
| 131 |
if prompt := st.chat_input("How may I help you learn math today?"):
|
| 132 |
# Add user message to chat history
|
| 133 |
+
st.session_state.messages.append({"role": "user", "content": prompt,"images":[data]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
# Display user message in chat message container
|
| 135 |
with st.chat_message("user"):
|
| 136 |
st.markdown(prompt)
|
| 137 |
|
| 138 |
with st.chat_message(st.session_state.model):
|
| 139 |
logger.info(f"Message to {st.session_state.model}: {st.session_state.messages[-1]}")
|
| 140 |
+
response = st.write(hf_generator(
|
| 141 |
st.session_state.model,
|
| 142 |
+
prompt,
|
| 143 |
+
data,
|
| 144 |
+
system
|
| 145 |
))
|
|
|
|
|
|
|
| 146 |
|