File size: 2,438 Bytes
7262082
 
 
 
 
 
 
 
 
 
 
 
 
 
e5075e9
7262082
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4dfdd0e
7262082
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.document_loaders import TextLoader
from langchain.chains import RetrievalQA
from langchain.llms.base import LLM

from typing import List, Optional
from groq import Groq
import os



loader = TextLoader("./Project.txt")
documents = loader.load()

text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
docs = text_splitter.split_documents(documents)


embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
vectorstore = Chroma.from_documents(docs, embedding, persist_directory="rag_chroma_groq")



class GroqLLM(LLM):
    model: str = "llama3-8b-8192"
    api_key: str = "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"  # Replace with your actual API key
    temperature: float = 0.0

    def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
        client = Groq(api_key=self.api_key)

        messages = [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": prompt}
        ]

        response = client.chat.completions.create(
            model=self.model,
            messages=messages,
            temperature=self.temperature,
        )

        return response.choices[0].message.content

    @property
    def _llm_type(self) -> str:
        return "groq-llm"



retriever = vectorstore.as_retriever()
groq_llm = GroqLLM(api_key="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")

qa_chain = RetrievalQA.from_chain_type(
    llm=groq_llm,
    retriever=retriever,
    return_source_documents=True
)



query = "Explain the whole project in points and sections"
result = qa_chain({"query": query})
print("Answer:", result["result"])

import gradio as gr

# Ensure qa_chain is defined (from your code above)

# Define the function that will be called when the user submits a question
def answer_query(query):
    result = qa_chain({"query": query})
    return result["result"]

# Create the Gradio interface
interface = gr.Interface(
    fn=answer_query,
    inputs=gr.Textbox(lines=2, placeholder="Ask me anything about the project..."),
    outputs="text",
    title="🧠 Project Summariser",
    description="Ask questions based on my projects"
)

# Launch the interface
interface.launch()