Spaces:
Runtime error
Runtime error
Abid Ali Awan
commited on
Commit
·
27ac14c
1
Parent(s):
a6d1d2c
Realtime AI RAG app
Browse files- .gitattributes +1 -0
- README.md +4 -3
- Starwars_Vectordb/c4319e40-03fd-4cf7-b946-82b84e796825/data_level0.bin +3 -0
- Starwars_Vectordb/c4319e40-03fd-4cf7-b946-82b84e796825/header.bin +3 -0
- Starwars_Vectordb/c4319e40-03fd-4cf7-b946-82b84e796825/length.bin +3 -0
- Starwars_Vectordb/c4319e40-03fd-4cf7-b946-82b84e796825/link_lists.bin +0 -0
- Starwars_Vectordb/chroma.sqlite3 +3 -0
- app.py +73 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 1 |
+
*.sqlite3 filter=lfs diff=lfs merge=lfs -text
|
| 2 |
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 3 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 4 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
---
|
| 2 |
-
title: Real Time RAG
|
| 3 |
emoji: 📉
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.42.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Real Time RAG Application
|
| 3 |
emoji: 📉
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.42.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
| 11 |
+
description: Real-time AI App with Groq API and LangChain
|
| 12 |
---
|
| 13 |
|
| 14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
Starwars_Vectordb/c4319e40-03fd-4cf7-b946-82b84e796825/data_level0.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:95042e844cfb77b20e578cf65635282a99d7c4dd20e589ac062f38bc389f8e58
|
| 3 |
+
size 4236000
|
Starwars_Vectordb/c4319e40-03fd-4cf7-b946-82b84e796825/header.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fcc596bc1909f7cc610d5839236c90513b4fbad06776c253fa1b21bfd712e940
|
| 3 |
+
size 100
|
Starwars_Vectordb/c4319e40-03fd-4cf7-b946-82b84e796825/length.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:08ed7b91d9d7ca5434195ba03bfe5aeacbb387ea140381f7df3c1c02cd3dd8b0
|
| 3 |
+
size 4000
|
Starwars_Vectordb/c4319e40-03fd-4cf7-b946-82b84e796825/link_lists.bin
ADDED
|
File without changes
|
Starwars_Vectordb/chroma.sqlite3
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b0962e37a1d9b0257f9b24ddd980ae20301e347edba66bb0ef9d84c15a0dbe8d
|
| 3 |
+
size 3264512
|
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 4 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 5 |
+
from langchain_groq import ChatGroq
|
| 6 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 7 |
+
from langchain_chroma import Chroma
|
| 8 |
+
from langchain_core.prompts import PromptTemplate
|
| 9 |
+
|
| 10 |
+
# Load the API key from environment variables
|
| 11 |
+
groq_api_key = os.getenv("Groq_API_Key")
|
| 12 |
+
|
| 13 |
+
# Initialize the language model with the specified model and 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(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
| 18 |
+
|
| 19 |
+
# Load the vector store from a local directory
|
| 20 |
+
vectorstore = Chroma(
|
| 21 |
+
"Starwars_Vectordb",
|
| 22 |
+
embedding=embed_model,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Convert the vector store to a retriever
|
| 26 |
+
retriever = vectorstore.as_retriever()
|
| 27 |
+
|
| 28 |
+
# Define the prompt template for the language model
|
| 29 |
+
template = """You are a Star Wars assistant for answering questions.
|
| 30 |
+
Use the provided context to answer the question.
|
| 31 |
+
If you don't know the answer, say so. Explain your answer in detail.
|
| 32 |
+
Do not discuss the context in your response; just provide the answer directly.
|
| 33 |
+
|
| 34 |
+
Context: {context}
|
| 35 |
+
|
| 36 |
+
Question: {question}
|
| 37 |
+
|
| 38 |
+
Answer:"""
|
| 39 |
+
|
| 40 |
+
rag_prompt = PromptTemplate.from_template(template)
|
| 41 |
+
|
| 42 |
+
# Create the RAG (Retrieval-Augmented Generation) chain
|
| 43 |
+
rag_chain = (
|
| 44 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
| 45 |
+
| rag_prompt
|
| 46 |
+
| llm
|
| 47 |
+
| StrOutputParser()
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Define the function to stream the RAG memory
|
| 51 |
+
def rag_memory_stream(text):
|
| 52 |
+
partial_text = ""
|
| 53 |
+
for new_text in rag_chain.stream(text):
|
| 54 |
+
partial_text += new_text
|
| 55 |
+
# Yield the updated conversation history
|
| 56 |
+
yield partial_text
|
| 57 |
+
|
| 58 |
+
# Set up the Gradio interface
|
| 59 |
+
title = "Real-time AI App with Groq API and LangChain"
|
| 60 |
+
demo = gr.Interface(
|
| 61 |
+
title=title,
|
| 62 |
+
fn=rag_memory_stream,
|
| 63 |
+
inputs="text",
|
| 64 |
+
outputs="text",
|
| 65 |
+
live=True,
|
| 66 |
+
batch=True,
|
| 67 |
+
max_batch_size=10000,
|
| 68 |
+
concurrency_limit=16,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Launch the Gradio interface
|
| 72 |
+
demo.queue()
|
| 73 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
langchain-community
|
| 3 |
+
langchainhub
|
| 4 |
+
langchain-chroma
|
| 5 |
+
langchain-groq
|
| 6 |
+
langchain-huggingface
|