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

experiment with input text

Browse files
Files changed (1) hide show
  1. app.py +30 -16
app.py CHANGED
@@ -51,22 +51,26 @@ rag_chain = (
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
72
  title = "Real-time AI App with Groq API and LangChain"
@@ -76,18 +80,28 @@ description = """
76
  </center>
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
 
51
 
52
 
53
  # Define the function to stream the RAG memory
54
+ def rag_memory_stream(text, change_tracker):
55
+ if change_tracker.get("changed", False):
56
+ return # Stop the generation if input has changed
 
 
 
57
 
58
+ partial_text = ""
59
  for new_text in rag_chain.stream(text):
60
+ if change_tracker.get("changed", False):
61
+ return # Stop the generation if input has changed
 
62
  partial_text += new_text
63
+ yield partial_text # Yield the updated conversation history
64
 
 
65
 
66
+ def input_listener(text, change_tracker):
67
+ change_tracker["changed"] = True
68
+ change_tracker["changed"] = False
69
+ return text
70
+
71
+
72
+ # Initialize a change tracker
73
+ change_tracker = {"changed": False}
74
 
75
  # Set up the Gradio interface
76
  title = "Real-time AI App with Groq API and LangChain"
 
80
  </center>
81
  """
82
 
83
+ # Define input components with event listeners
84
+ text_input = gr.Textbox(label="Enter your question", elem_id="question")
85
+ text_input.change(
86
+ fn=input_listener,
87
+ inputs=[text_input],
88
+ outputs=[text_input],
89
+ change_tracker=change_tracker,
90
+ )
91
+
92
+ # Create the Gradio interface
93
  demo = gr.Interface(
94
+ title=title,
95
+ description=description,
96
+ fn=lambda text: rag_memory_stream(text, change_tracker),
97
+ inputs=text_input,
98
+ outputs="text",
99
  live=True,
100
  batch=True,
101
  max_batch_size=10000,
102
  concurrency_limit=12,
103
  allow_flagging="never",
104
  theme=gr.themes.Soft(),
 
 
105
  )
106
 
107
  # Launch the Gradio interface