Abid Ali Awan commited on
Commit
90708d3
·
1 Parent(s): dfabd41

improve ment in the live feature

Browse files
Files changed (1) hide show
  1. app.py +13 -26
app.py CHANGED
@@ -49,33 +49,23 @@ rag_chain = (
49
  | StrOutputParser()
50
  )
51
 
52
- # Global variable to store the last input text
53
- last_input_text = ""
54
-
55
 
56
  # Define the function to stream the RAG memory
57
- def rag_memory_stream(text):
58
- global last_input_text
59
  partial_text = ""
60
- last_input_text = text # Set the initial text
 
 
 
61
 
62
  for new_text in rag_chain.stream(text):
63
- # Check if the text has changed
64
  if text != last_input_text:
65
- # If input has changed, break the loop to stop generation
66
- break
67
  partial_text += new_text
68
- # Yield the updated conversation history
69
  yield partial_text
70
 
71
- # Update last_input_text after processing
72
- last_input_text = text
73
-
74
-
75
- # Function to update the last input text
76
- def update_input(text):
77
- global last_input_text
78
- last_input_text = text
79
 
80
 
81
  # Set up the Gradio interface
@@ -87,22 +77,19 @@ description = """
87
  """
88
 
89
  demo = gr.Interface(
90
- title=title,
91
- description=description,
92
  fn=rag_memory_stream,
93
- inputs="text",
94
- outputs="text",
95
  live=True,
96
  batch=True,
97
  max_batch_size=10000,
98
  concurrency_limit=12,
99
  allow_flagging="never",
100
  theme=gr.themes.Soft(),
 
 
101
  )
102
 
103
- # Register the input update function
104
- demo.input_event(update_input)
105
-
106
  # Launch the Gradio interface
107
  demo.queue()
108
- demo.launch()
 
49
  | StrOutputParser()
50
  )
51
 
 
 
 
52
 
53
  # Define the function to stream the RAG memory
54
+ def rag_memory_stream(text, last_input_text=""):
 
55
  partial_text = ""
56
+
57
+ # If the text has changed, reset the partial_text and regenerate
58
+ if text != last_input_text:
59
+ partial_text = "" # Clear partial text when input changes
60
 
61
  for new_text in rag_chain.stream(text):
62
+ # If input changes, reset generation
63
  if text != last_input_text:
64
+ break # Stop current generation if text has changed
 
65
  partial_text += new_text
 
66
  yield partial_text
67
 
68
+ return partial_text, text # Return updated text for Gradio state
 
 
 
 
 
 
 
69
 
70
 
71
  # Set up the Gradio interface
 
77
  """
78
 
79
  demo = gr.Interface(
 
 
80
  fn=rag_memory_stream,
81
+ inputs=["text", "state"],
82
+ outputs=["text", "state"],
83
  live=True,
84
  batch=True,
85
  max_batch_size=10000,
86
  concurrency_limit=12,
87
  allow_flagging="never",
88
  theme=gr.themes.Soft(),
89
+ title=title,
90
+ description=description,
91
  )
92
 
 
 
 
93
  # Launch the Gradio interface
94
  demo.queue()
95
+ demo.launch()