Update app.py
Browse files
app.py
CHANGED
|
@@ -72,39 +72,38 @@ client = InferenceClient(token=os.environ["HUGGINGFACEHUB_API_TOKEN"])
|
|
| 72 |
|
| 73 |
def respond(message, history: list[dict]):
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
history.append({"role": "user", "content": message})
|
| 79 |
|
| 80 |
-
# invoke the agent
|
| 81 |
agent_output = agent_executor.invoke({
|
| 82 |
"query": message,
|
| 83 |
-
"chat_history":
|
| 84 |
-
|
| 85 |
-
raw = agent_output["output"]
|
| 86 |
-
|
| 87 |
-
# try to JSON-parse and format
|
| 88 |
-
try:
|
| 89 |
-
out = parser.parse(raw)
|
| 90 |
-
body = " ".join([out.empathetic_response,
|
| 91 |
-
out.informative_response,
|
| 92 |
-
out.quran]).strip()
|
| 93 |
-
# split sentences onto their own lines
|
| 94 |
-
sentences = [
|
| 95 |
-
s.strip()
|
| 96 |
-
for s in re.split(r'(?<=[.!?])\s+', body)
|
| 97 |
-
if s.strip()
|
| 98 |
]
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
# 5) launch ChatInterface
|
| 110 |
demo = gr.ChatInterface(
|
|
|
|
| 72 |
|
| 73 |
def respond(message, history: list[dict]):
|
| 74 |
|
| 75 |
+
messages = history + [{"role": "user", "content": message}]
|
| 76 |
+
|
| 77 |
+
print(messages)
|
|
|
|
| 78 |
|
|
|
|
| 79 |
agent_output = agent_executor.invoke({
|
| 80 |
"query": message,
|
| 81 |
+
"chat_history": [
|
| 82 |
+
{"role": m.role, "content": m.content} for m in history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
]
|
| 84 |
+
})
|
| 85 |
+
# parse its JSON output
|
| 86 |
+
out = parser.parse(agent_output["output"])
|
| 87 |
+
|
| 88 |
+
response = {"role": "assistant", "content": "\n".join([
|
| 89 |
+
out.empathetic_response,
|
| 90 |
+
out.informative_response,
|
| 91 |
+
out.quran,
|
| 92 |
+
out.question,
|
| 93 |
+
])}
|
| 94 |
+
|
| 95 |
+
for message in client.chat_completion(
|
| 96 |
+
messages,
|
| 97 |
+
model="HuggingFaceH4/zephyr-7b-beta",
|
| 98 |
+
max_tokens=512,
|
| 99 |
+
stream=True,
|
| 100 |
+
temperature=0.7,
|
| 101 |
+
top_p=0.95,
|
| 102 |
+
):
|
| 103 |
+
token = message.choices[0].delta.content
|
| 104 |
+
|
| 105 |
+
#response['content'] += token
|
| 106 |
+
yield response
|
| 107 |
|
| 108 |
# 5) launch ChatInterface
|
| 109 |
demo = gr.ChatInterface(
|