SumbalFatima1122 commited on
Commit
9134b7a
·
verified ·
1 Parent(s): 8c22b51

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import os
3
+ import pandas as pd
4
+ from sentence_transformers import SentenceTransformer
5
+ import faiss
6
+ import numpy as np
7
+ import gradio as gr
8
+ from groq import Groq
9
+
10
+ # Set up Groq API
11
+ GROQ_API = "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" # Replace with your Groq API key
12
+ client = Groq(api_key=GROQ_API)
13
+
14
+ # # Load environmental dataset (upload to Colab)
15
+ # from google.colab import files
16
+ # uploaded = files.upload() # Upload 'environmental_impact_assessment_dataset.csv'
17
+
18
+ # Load the dataset
19
+ df = pd.read_csv('/content/environmental_impact_assessment_dataset.csv') # Replace with the uploaded file name
20
+
21
+ # Combine relevant text columns for embeddings
22
+ text_column = df['Project Type'] + ' ' + df['Mitigation Measures'] # Adjust based on your dataset columns
23
+
24
+ # Use SentenceTransformers to generate text embeddings
25
+ embedding_model = SentenceTransformer('all-MiniLM-L6-v2') # Lightweight embedding model
26
+ embeddings = embedding_model.encode(text_column.tolist())
27
+
28
+ # Convert embeddings to numpy array
29
+ embeddings_np = np.array(embeddings).astype(np.float32)
30
+
31
+ # Build FAISS index for document retrieval
32
+ index = faiss.IndexFlatL2(embeddings_np.shape[1]) # L2 distance for similarity search
33
+ index.add(embeddings_np) # Add the document embeddings to the FAISS index
34
+
35
+ # Function to retrieve the most relevant document from the dataset
36
+ def retrieve_relevant_document(query):
37
+ # Generate query embedding
38
+ query_embedding = embedding_model.encode([query])
39
+ query_embedding_np = np.array(query_embedding).astype(np.float32)
40
+
41
+ # Perform similarity search in FAISS
42
+ _, indices = index.search(query_embedding_np, k=1) # Top 1 match
43
+ retrieved_text = text_column.iloc[indices[0][0]] # Retrieve corresponding text
44
+
45
+ return retrieved_text
46
+
47
+ # Function to generate an EIA report using Groq's API
48
+ def generate_report(user_input):
49
+ # Check if input is empty
50
+ if not user_input.strip():
51
+ return "Please provide project details to generate the Environmental Impact Assessment report."
52
+
53
+ # Retrieve relevant information using FAISS
54
+ relevant_document = retrieve_relevant_document(user_input)
55
+
56
+ # Use Groq API to generate a report based on the retrieved document
57
+ chat_completion = client.chat.completions.create(
58
+ messages=[
59
+ {"role": "user",
60
+ "content": f"Generate an environmental impact assessment report based on the following details:\n\n{relevant_document}\n\nUser Query: {user_input}"}
61
+ ],
62
+ model="llama3-8b-8192", # Groq model
63
+ )
64
+
65
+ # Return the Groq-generated content
66
+ return chat_completion.choices[0].message.content
67
+
68
+ # Gradio interface for user interaction
69
+ def gradio_interface(project_details):
70
+ return generate_report(project_details)
71
+
72
+ # Launch Gradio app
73
+ iface = gr.Interface(
74
+ fn=gradio_interface,
75
+ inputs="text", # Input: text box for project details
76
+ outputs="text", # Output: text box for the generated report
77
+ live=False # Set to False for non-live mode
78
+ )
79
+
80
+ iface.launch()