Spaces:
Sleeping
Sleeping
Abid Ali Awan
commited on
Commit
·
dfabd41
1
Parent(s):
02a6a6d
working on the live version
Browse files
app.py
CHANGED
@@ -14,8 +14,9 @@ groq_api_key = os.getenv("Groq_API_Key")
|
|
14 |
llm = ChatGroq(model="llama-3.1-70b-versatile", api_key=groq_api_key)
|
15 |
|
16 |
# Initialize the embedding model
|
17 |
-
embed_model = HuggingFaceEmbeddings(
|
18 |
-
|
|
|
19 |
|
20 |
# Load the vector store from a local directory
|
21 |
vectorstore = Chroma(
|
@@ -48,14 +49,35 @@ rag_chain = (
|
|
48 |
| StrOutputParser()
|
49 |
)
|
50 |
|
|
|
|
|
|
|
|
|
51 |
# Define the function to stream the RAG memory
|
52 |
def rag_memory_stream(text):
|
|
|
53 |
partial_text = ""
|
|
|
|
|
54 |
for new_text in rag_chain.stream(text):
|
|
|
|
|
|
|
|
|
55 |
partial_text += new_text
|
56 |
# Yield the updated conversation history
|
57 |
yield partial_text
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
# Set up the Gradio interface
|
60 |
title = "Real-time AI App with Groq API and LangChain"
|
61 |
description = """
|
@@ -78,6 +100,9 @@ demo = gr.Interface(
|
|
78 |
theme=gr.themes.Soft(),
|
79 |
)
|
80 |
|
|
|
|
|
|
|
81 |
# Launch the Gradio interface
|
82 |
demo.queue()
|
83 |
demo.launch()
|
|
|
14 |
llm = ChatGroq(model="llama-3.1-70b-versatile", api_key=groq_api_key)
|
15 |
|
16 |
# Initialize the embedding model
|
17 |
+
embed_model = HuggingFaceEmbeddings(
|
18 |
+
model_name="mixedbread-ai/mxbai-embed-large-v1", model_kwargs={"device": "cpu"}
|
19 |
+
)
|
20 |
|
21 |
# Load the vector store from a local directory
|
22 |
vectorstore = Chroma(
|
|
|
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
|
82 |
title = "Real-time AI App with Groq API and LangChain"
|
83 |
description = """
|
|
|
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()
|