Spaces:
Sleeping
Sleeping
| 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() | |