File size: 2,078 Bytes
426f73b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75c0712
426f73b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
from openai import OpenAI
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
import os
from groq import Groq

# Load Excel Data
data_file = "project_data.xlsx"  # Replace with your actual file
df = pd.read_excel(data_file)
df['combined_text'] = df.apply(lambda row: ' '.join(row.astype(str)), axis=1)

# Embedding Model and FAISS Index
model = SentenceTransformer("all-MiniLM-L6-v2")
embeddings = model.encode(df['combined_text'].tolist())
index = faiss.IndexFlatL2(embeddings.shape[1])
index.add(np.array(embeddings))

# Groq API Configuration
# api_key = "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"  # Replace with your API key


def generate_answer(query, top_k=3):
    client = Groq(
        api_key='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',
    )
    # Step 1: Embed Query
    query_embedding = model.encode([query])[0]

    # Step 2: Retrieve Relevant Data
    distances, indices = index.search(np.array([query_embedding]), top_k)
    relevant_data = df.iloc[indices[0]].to_dict(orient="records")

    # Step 3: Prepare Context
    context = "\n".join([str(row) for row in relevant_data])

    # Step 4: Generate Response using Groq API
    # prompt =
    response = client.chat.completions.create(
        model="llama-3.1-70b-versatile",  # Replace with the appropriate model
        # prompt=f"Generate a question based on this content: {chunk}",
        messages=[{
            "role": "user",
            "content": f"Answer the question based on the following context:\n{context}\n\nQuestion: {query}\nAnswer:",
        }],

        max_tokens=200
    )
    print(response.choices[0].message.content)
    return response.choices[0].message.content


# Gradio Interface
def gradio_interface(query):
    return generate_answer(query)


iface = gr.Interface(
    fn=gradio_interface,
    inputs="text",
    outputs="text",
    title="Project Data Q&A System",
    description="Ask questions about project data from the provided Excel sheet.",
)

iface.launch()