YZ03 commited on
Commit
dc2676a
·
verified ·
1 Parent(s): dc40e9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -29
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
- # history is now List[dict] with keys "role" and "content"
76
- history = history or []
77
- # append user turn
78
- history.append({"role": "user", "content": message})
79
 
80
- # invoke the agent
81
  agent_output = agent_executor.invoke({
82
  "query": message,
83
- "chat_history": history # pass the dict-list directly
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
- assistant_text = "\n".join(sentences + [out.question])
100
- except Exception:
101
- assistant_text = raw
102
-
103
- # append assistant turn
104
- history.append({"role": "assistant", "content": assistant_text})
105
-
106
- # return bot text + updated history
107
- return assistant_text, history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(