ITookAPill commited on
Commit
04820fc
·
verified ·
1 Parent(s): cae007c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -24
app.py CHANGED
@@ -1,12 +1,11 @@
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,17 +13,29 @@ def format_context(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,20 +43,12 @@ 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,13 +58,22 @@ def main():
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()
 
 
1
  import gradio as gr
2
  import spaces
3
  from dotenv import load_dotenv
4
+ # Ensure your implementation file exposes separate retrieval and generation functions
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
  result += doc.page_content + "\n\n"
14
  return result
15
 
16
+ # STEP 1: CPU-only step to fetch context immediately without waiting for a GPU slot
17
+ def retrieve_context_step(history):
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
+ # Run vector DB lookup or API calls on the CPU
26
+ context = get_context(last_message, prior, use_rewrite=True)
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
  return "", history + [{"role": "user", "content": message}]
44
 
45
  with gr.Blocks(title="PyComp: Simple Python Companion") as ui:
46
+ gr.Markdown("# 🏢 Meet PyComp: Simple Python Companion\nAsk me anything about Python!")
 
47
 
48
  with gr.Row():
49
  with gr.Column(scale=1):
50
+ chatbot = gr.Chatbot(label="💬 Conversation", height=600, type="messages")
51
+ message = gr.Textbox(label="Your Question", placeholder="Ask anything about Python", show_label=False)
 
 
 
 
 
 
 
52
 
53
  with gr.Column(scale=1):
54
  context_markdown = gr.Markdown(
 
58
  height=600,
59
  )
60
 
61
+ # Chain the events: First retrieve context on CPU, then stream generation on GPU
62
+ submit_event = message.submit(
63
+ put_message_in_chatbot,
64
+ inputs=[message, chatbot],
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()