Spaces:
Sleeping
Sleeping
Commit ·
5bf7e1a
1
Parent(s): a5d0a25
final
Browse files- app.py +65 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 4 |
+
from langchain_community.vectorstores import FAISS
|
| 5 |
+
from langchain_groq import ChatGroq
|
| 6 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 8 |
+
from langchain.prompts import PromptTemplate
|
| 9 |
+
from langchain.memory import ConversationBufferMemory
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
from langchain_community.document_loaders import TextLoader
|
| 12 |
+
|
| 13 |
+
load_dotenv()
|
| 14 |
+
|
| 15 |
+
# Load environment variables
|
| 16 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 17 |
+
|
| 18 |
+
# Set up LLM and embeddings
|
| 19 |
+
llm = ChatGroq(groq_api_key=GROQ_API_KEY, model_name="llama3-8b-8192")
|
| 20 |
+
embeddings = HuggingFaceEmbeddings()
|
| 21 |
+
|
| 22 |
+
# Load documents and create vectorstore
|
| 23 |
+
loader = TextLoader("sample.txt") # Put your content here
|
| 24 |
+
documents = loader.load()
|
| 25 |
+
db = FAISS.from_documents(documents, embeddings)
|
| 26 |
+
|
| 27 |
+
# Prompt template
|
| 28 |
+
template = """
|
| 29 |
+
You are a helpful assistant. Use the following conversation and context to answer the question.
|
| 30 |
+
If you don't know the answer, just say you don't know.
|
| 31 |
+
|
| 32 |
+
Context: {context}
|
| 33 |
+
Conversation History: {chat_history}
|
| 34 |
+
Question: {question}
|
| 35 |
+
|
| 36 |
+
Helpful answer:
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
prompt = PromptTemplate(input_variables=["context", "chat_history", "question"], template=template)
|
| 40 |
+
|
| 41 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 42 |
+
|
| 43 |
+
qa_chain = ConversationalRetrievalChain.from_llm(
|
| 44 |
+
llm=llm,
|
| 45 |
+
retriever=db.as_retriever(),
|
| 46 |
+
memory=memory,
|
| 47 |
+
condense_question_prompt=prompt,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Gradio UI
|
| 51 |
+
def chat_with_voice(audio):
|
| 52 |
+
text = audio # audio input is transcribed to text
|
| 53 |
+
result = qa_chain.invoke({"question": text})
|
| 54 |
+
return result['answer']
|
| 55 |
+
|
| 56 |
+
iface = gr.Interface(
|
| 57 |
+
fn=chat_with_voice,
|
| 58 |
+
inputs=gr.Audio(source="microphone", type="filepath", label="Speak Your Question"),
|
| 59 |
+
outputs="text",
|
| 60 |
+
title="Voice-Powered LLM Chatbot",
|
| 61 |
+
description="Talk to a chatbot using your voice. Backed by Groq's Llama 3 and vector search.",
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
langchain
|
| 3 |
+
langchain-community
|
| 4 |
+
langchain-groq
|
| 5 |
+
faiss-cpu
|
| 6 |
+
sentence-transformers
|
| 7 |
+
python-dotenv
|
| 8 |
+
groq
|