Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
-
|
| 5 |
-
from implementation.answer import get_context, generate_response_stream
|
| 6 |
|
| 7 |
load_dotenv(override=True)
|
| 8 |
|
|
|
|
| 9 |
def format_context(context):
|
| 10 |
result = "<h2 style='color: #ff7800;'>Relevant Context</h2>\n\n"
|
| 11 |
for doc in context:
|
|
@@ -13,29 +14,17 @@ def format_context(context):
|
|
| 13 |
result += doc.page_content + "\n\n"
|
| 14 |
return result
|
| 15 |
|
| 16 |
-
|
| 17 |
-
def
|
| 18 |
last_message = (
|
| 19 |
"\n".join(map(str, history[-1]["content"]))
|
| 20 |
if isinstance(history[-1]["content"], list)
|
| 21 |
else history[-1]["content"]
|
| 22 |
)
|
| 23 |
prior = history[:-1]
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return format_context(context)
|
| 28 |
-
|
| 29 |
-
# STEP 2: Dedicated GPU step that only kicks in for actual model inference
|
| 30 |
-
@spaces.GPU
|
| 31 |
-
def generate_answer_step(history, context_html):
|
| 32 |
-
# Append placeholder for assistant response
|
| 33 |
-
history.append({"role": "assistant", "content": ""})
|
| 34 |
-
|
| 35 |
-
# Ensure your LLM function yields chunks of text (Streaming)
|
| 36 |
-
for text_chunk in generate_response_stream(history[:-1], context_html):
|
| 37 |
-
history[-1]["content"] = text_chunk
|
| 38 |
-
yield history
|
| 39 |
|
| 40 |
|
| 41 |
def main():
|
|
@@ -43,12 +32,20 @@ def main():
|
|
| 43 |
return "", history + [{"role": "user", "content": message}]
|
| 44 |
|
| 45 |
with gr.Blocks(title="PyComp: Simple Python Companion") as ui:
|
| 46 |
-
gr.Markdown(
|
|
|
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
with gr.Column(scale=1):
|
| 50 |
-
chatbot = gr.Chatbot(
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
with gr.Column(scale=1):
|
| 54 |
context_markdown = gr.Markdown(
|
|
@@ -58,22 +55,13 @@ def main():
|
|
| 58 |
height=600,
|
| 59 |
)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
outputs=[message, chatbot]
|
| 66 |
-
).then(
|
| 67 |
-
retrieve_context_step,
|
| 68 |
-
inputs=[chatbot],
|
| 69 |
-
outputs=[context_markdown]
|
| 70 |
-
).then(
|
| 71 |
-
generate_answer_step,
|
| 72 |
-
inputs=[chatbot, context_markdown],
|
| 73 |
-
outputs=[chatbot]
|
| 74 |
-
)
|
| 75 |
|
| 76 |
ui.launch()
|
| 77 |
|
|
|
|
| 78 |
if __name__ == "__main__":
|
| 79 |
-
main()
|
|
|
|
| 1 |
+
# Import Libraries
|
| 2 |
import gradio as gr
|
| 3 |
import spaces
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
+
from implementation.answer import answer_question
|
|
|
|
| 6 |
|
| 7 |
load_dotenv(override=True)
|
| 8 |
|
| 9 |
+
|
| 10 |
def format_context(context):
|
| 11 |
result = "<h2 style='color: #ff7800;'>Relevant Context</h2>\n\n"
|
| 12 |
for doc in context:
|
|
|
|
| 14 |
result += doc.page_content + "\n\n"
|
| 15 |
return result
|
| 16 |
|
| 17 |
+
@spaces.GPU
|
| 18 |
+
def chat(history):
|
| 19 |
last_message = (
|
| 20 |
"\n".join(map(str, history[-1]["content"]))
|
| 21 |
if isinstance(history[-1]["content"], list)
|
| 22 |
else history[-1]["content"]
|
| 23 |
)
|
| 24 |
prior = history[:-1]
|
| 25 |
+
answer, context = answer_question(last_message, prior, use_rewrite=True)
|
| 26 |
+
history.append({"role": "assistant", "content": answer})
|
| 27 |
+
return history, format_context(context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
def main():
|
|
|
|
| 32 |
return "", history + [{"role": "user", "content": message}]
|
| 33 |
|
| 34 |
with gr.Blocks(title="PyComp: Simple Python Companion") as ui:
|
| 35 |
+
gr.Markdown(
|
| 36 |
+
"# 🏢 Meet PyComp: Simple Python Companion\nAsk me anything about Python!")
|
| 37 |
|
| 38 |
with gr.Row():
|
| 39 |
with gr.Column(scale=1):
|
| 40 |
+
chatbot = gr.Chatbot(
|
| 41 |
+
label="💬 Conversation",
|
| 42 |
+
height=600,
|
| 43 |
+
)
|
| 44 |
+
message = gr.Textbox(
|
| 45 |
+
label="Your Question",
|
| 46 |
+
placeholder="Ask anything about Python",
|
| 47 |
+
show_label=False,
|
| 48 |
+
)
|
| 49 |
|
| 50 |
with gr.Column(scale=1):
|
| 51 |
context_markdown = gr.Markdown(
|
|
|
|
| 55 |
height=600,
|
| 56 |
)
|
| 57 |
|
| 58 |
+
message.submit(
|
| 59 |
+
put_message_in_chatbot, inputs=[
|
| 60 |
+
message, chatbot], outputs=[message, chatbot]
|
| 61 |
+
).then(chat, inputs=chatbot, outputs=[chatbot, context_markdown])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
ui.launch()
|
| 64 |
|
| 65 |
+
|
| 66 |
if __name__ == "__main__":
|
| 67 |
+
main()
|