Spaces:
Sleeping
Sleeping
Commit
·
78654a1
1
Parent(s):
e71ef7a
Added streaming to the chat
Browse files- api/llm.py +22 -6
- app.py +3 -1
api/llm.py
CHANGED
|
@@ -22,9 +22,11 @@ class LLMManager:
|
|
| 22 |
if self.streaming:
|
| 23 |
self.end_interview = self.end_interview_stream
|
| 24 |
self.get_problem = self.get_problem_stream
|
|
|
|
| 25 |
else:
|
| 26 |
self.end_interview = self.end_interview_full
|
| 27 |
self.get_problem = self.get_problem_full
|
|
|
|
| 28 |
|
| 29 |
def text_processor(self):
|
| 30 |
def ans_full(response):
|
|
@@ -120,20 +122,34 @@ class LLMManager:
|
|
| 120 |
messages = self.get_problem_prepare_messages(requirements, difficulty, topic)
|
| 121 |
yield from self.get_text_stream(messages)
|
| 122 |
|
| 123 |
-
def
|
| 124 |
if code != previous_code:
|
| 125 |
chat_history.append({"role": "user", "content": f"My latest code:\n{code}"})
|
| 126 |
chat_history.append({"role": "user", "content": message})
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
reply = self.get_text(chat_history)
|
|
|
|
| 129 |
chat_history.append({"role": "assistant", "content": reply})
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
|
| 137 |
|
| 138 |
def end_interview_prepare_messages(self, problem_description, chat_history):
|
| 139 |
transcript = [f"{message['role'].capitalize()}: {message['content']}" for message in chat_history[1:]]
|
|
|
|
| 22 |
if self.streaming:
|
| 23 |
self.end_interview = self.end_interview_stream
|
| 24 |
self.get_problem = self.get_problem_stream
|
| 25 |
+
self.send_request = self.send_request_stream
|
| 26 |
else:
|
| 27 |
self.end_interview = self.end_interview_full
|
| 28 |
self.get_problem = self.get_problem_full
|
| 29 |
+
self.send_request = self.send_request_full
|
| 30 |
|
| 31 |
def text_processor(self):
|
| 32 |
def ans_full(response):
|
|
|
|
| 122 |
messages = self.get_problem_prepare_messages(requirements, difficulty, topic)
|
| 123 |
yield from self.get_text_stream(messages)
|
| 124 |
|
| 125 |
+
def update_chat_history(self, code, previous_code, message, chat_history):
|
| 126 |
if code != previous_code:
|
| 127 |
chat_history.append({"role": "user", "content": f"My latest code:\n{code}"})
|
| 128 |
chat_history.append({"role": "user", "content": message})
|
| 129 |
|
| 130 |
+
return chat_history
|
| 131 |
+
|
| 132 |
+
def send_request_full(self, code, previous_code, message, chat_history, chat_display):
|
| 133 |
+
chat_history = self.update_chat_history(code, previous_code, message, chat_history)
|
| 134 |
+
|
| 135 |
reply = self.get_text(chat_history)
|
| 136 |
+
chat_display.append([None, reply])
|
| 137 |
chat_history.append({"role": "assistant", "content": reply})
|
| 138 |
|
| 139 |
+
return chat_history, chat_display, code
|
| 140 |
+
|
| 141 |
+
def send_request_stream(self, code, previous_code, message, chat_history, chat_display):
|
| 142 |
+
chat_history = self.update_chat_history(code, previous_code, message, chat_history)
|
| 143 |
+
|
| 144 |
+
chat_display.append([None, ""])
|
| 145 |
+
chat_history.append({"role": "assistant", "content": ""})
|
| 146 |
+
|
| 147 |
+
reply = self.get_text_stream(chat_history)
|
| 148 |
+
for message in reply:
|
| 149 |
+
chat_display[-1][1] = message
|
| 150 |
+
chat_history[-1]["content"] = message
|
| 151 |
|
| 152 |
+
yield chat_history, chat_display, code
|
| 153 |
|
| 154 |
def end_interview_prepare_messages(self, problem_description, chat_history):
|
| 155 |
transcript = [f"{message['role'].capitalize()}: {message['content']}" for message in chat_history[1:]]
|
app.py
CHANGED
|
@@ -171,9 +171,11 @@ with gr.Blocks(title="AI Interviewer") as demo:
|
|
| 171 |
).then(fn=add_candidate_message, inputs=[message, chat], outputs=[chat]).then(
|
| 172 |
fn=llm.send_request,
|
| 173 |
inputs=[code, previous_code, message, chat_history, chat],
|
| 174 |
-
outputs=[chat_history, chat,
|
| 175 |
).then(
|
| 176 |
fn=tts.read_last_message, inputs=[chat], outputs=[audio_output]
|
|
|
|
|
|
|
| 177 |
)
|
| 178 |
|
| 179 |
demo.launch(show_api=False)
|
|
|
|
| 171 |
).then(fn=add_candidate_message, inputs=[message, chat], outputs=[chat]).then(
|
| 172 |
fn=llm.send_request,
|
| 173 |
inputs=[code, previous_code, message, chat_history, chat],
|
| 174 |
+
outputs=[chat_history, chat, previous_code],
|
| 175 |
).then(
|
| 176 |
fn=tts.read_last_message, inputs=[chat], outputs=[audio_output]
|
| 177 |
+
).then(
|
| 178 |
+
fn=lambda: "", outputs=[message]
|
| 179 |
)
|
| 180 |
|
| 181 |
demo.launch(show_api=False)
|