ogflash commited on
Commit
9c8b7bf
·
verified ·
1 Parent(s): 337d4b8

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +128 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """app.py
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1mhabOf4-2l1cLqd8jiKPDx-5NYSCi7gx
8
+ """
9
+
10
+ import gradio as gr
11
+ import os
12
+ from typing import List, Optional
13
+
14
+ from langchain_community.embeddings import HuggingFaceEmbeddings
15
+ from langchain_community.vectorstores import Chroma
16
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
17
+ from langchain.document_loaders import TextLoader
18
+ from langchain.chains import RetrievalQA
19
+ from langchain.llms.base import LLM
20
+ from groq import Groq
21
+
22
+ # Ensure Groq API key is set as an environment variable for Hugging Face Spaces compatibility
23
+ # For local testing, you can uncomment and replace with your key, or set it in your environment.
24
+ os.environ["GROQ_API_KEY"] = "YOUR_GROQ_API_KEY" # Replace with your actual API key if not using env var
25
+
26
+ # --- RAG Pipeline Setup (from your provided code) ---
27
+
28
+ # Step 1: Load Sample README File
29
+ sample_text = '''# Sample Project
30
+
31
+ This project demonstrates an example of a LangChain-powered RAG pipeline. It uses FAISS for vector search and a GROQ-hosted LLaMA3 model for response generation.
32
+
33
+ ## Features
34
+
35
+ - Document embedding
36
+ - Vector similarity search
37
+ - LLM-based QA over documents
38
+ '''
39
+
40
+ # Create a dummy file for the loader, as TextLoader expects a file path
41
+ with open("sample_readme.txt", "w") as f:
42
+ f.write(sample_text)
43
+
44
+ loader = TextLoader("sample_readme.txt")
45
+ documents = loader.load()
46
+
47
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
48
+ docs = text_splitter.split_documents(documents)
49
+
50
+ # Step 2: Create Embeddings & Store in Chroma
51
+ # For Hugging Face Spaces, ensure the model is downloaded and accessible.
52
+ # persist_directory ensures that the vectorstore is saved and can be reloaded.
53
+ embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
54
+ vectorstore = Chroma.from_documents(docs, embedding, persist_directory="rag_chroma_groq")
55
+
56
+ # Step 3: Define GROQ LLM Wrapper
57
+ class GroqLLM(LLM):
58
+ model: str = "llama3-8b-8192"
59
+ # Fetch API key from environment variable
60
+ api_key: str = os.getenv("GROQ_API_KEY")
61
+ temperature: float = 0.0
62
+
63
+ def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
64
+ if not self.api_key:
65
+ raise ValueError("GROQ_API_KEY environment variable not set.")
66
+ client = Groq(api_key=self.api_key)
67
+
68
+ messages = [
69
+ {"role": "system", "content": "You are a helpful assistant."},
70
+ {"role": "user", "content": prompt}
71
+ ]
72
+
73
+ response = client.chat.completions.create(
74
+ model=self.model,
75
+ messages=messages,
76
+ temperature=self.temperature,
77
+ )
78
+
79
+ return response.choices[0].message.content
80
+
81
+ @property
82
+ def _llm_type(self) -> str:
83
+ return "groq-llm"
84
+
85
+ # Step 4: Build RAG Pipeline with GROQ
86
+ # Check if GROQ_API_KEY is set before initializing GroqLLM
87
+ if os.getenv("GROQ_API_KEY"):
88
+ groq_llm = GroqLLM()
89
+ retriever = vectorstore.as_retriever()
90
+ qa_chain = RetrievalQA.from_chain_type(
91
+ llm=groq_llm,
92
+ retriever=retriever,
93
+ return_source_documents=True
94
+ )
95
+ else:
96
+ qa_chain = None # Set to None if API key is not available
97
+
98
+ # --- Gradio UI Implementation ---
99
+
100
+ def rag_query(query: str) -> str:
101
+ """
102
+ Function to handle RAG queries through the Gradio interface.
103
+ """
104
+ if not qa_chain:
105
+ return "Error: GROQ_API_KEY is not set. Please set it as an environment variable."
106
+ try:
107
+ result = qa_chain({"query": query})
108
+ answer = result["result"]
109
+ # Optionally, you can also return source documents if needed
110
+ # sources = "\n\nSource Documents:\n" + "\n".join([doc.page_content for doc in result["source_documents"]])
111
+ # return answer + sources
112
+ return answer
113
+ except Exception as e:
114
+ return f"An error occurred: {str(e)}"
115
+
116
+ # Define the Gradio interface
117
+ iface = gr.Interface(
118
+ fn=rag_query,
119
+ inputs=gr.Textbox(lines=2, placeholder="Enter your query here..."),
120
+ outputs="text",
121
+ title="RAG Pipeline with GROQ LLaMA3",
122
+ description="Ask questions about the sample project documentation and get answers from a GROQ-powered RAG system.",
123
+ allow_flagging="never" # Disable flagging for Hugging Face Spaces
124
+ )
125
+
126
+ # Launch the Gradio app
127
+ if __name__ == "__main__":
128
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ langchain
2
+ langchain-community
3
+ openai
4
+ chromadb
5
+ faiss-cpu
6
+ sentence-transformers
7
+ tiktoken
8
+ groq
9
+ gradio