SHAMIL SHAHBAZ AWAN commited on
Commit
381c281
·
verified ·
1 Parent(s): 79c0755

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -31
app.py CHANGED
@@ -2,8 +2,16 @@ import streamlit as st
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
4
  import seaborn as sns
5
- import json
6
- import requests
 
 
 
 
 
 
 
 
7
 
8
  # Configure page
9
  st.set_page_config(page_title="Data Augmentation App", layout="wide")
@@ -26,13 +34,16 @@ st.title("Data Augmentation and Analysis App")
26
  st.sidebar.title("Upload Your File")
27
  st.sidebar.markdown("Supported formats: CSV, Excel")
28
 
29
- # Get the Groq API key from secrets
30
  groq_api_key = st.secrets.get("HUGGINGFACE_KEY") # Fetch Groq API key stored in secrets.json
31
  if not groq_api_key:
32
  st.error("Groq API key not found in secrets.")
33
  else:
34
  st.success("Groq API Key Loaded Successfully!")
35
 
 
 
 
36
  # Function to load data from file (CSV/Excel)
37
  def load_file(uploaded_file):
38
  """Load the uploaded file."""
@@ -62,35 +73,18 @@ def generate_graph(data, query):
62
  st.error(f"Error generating graph: {e}")
63
 
64
  # Function to handle user queries using the Groq model
65
- def handle_query(data, query):
66
  """Handle user query using the Groq model."""
67
  try:
68
- # Groq API URL and Headers (adjust the URL to match your Groq API endpoint)
69
- api_url = "https://api.groq.com/v1/models/llama-3.1-8b-instant/generate"
70
- headers = {
71
- "Authorization": f"Bearer {groq_api_key}",
72
- "Content-Type": "application/json",
73
- }
74
-
75
- # Create prompt for the model
76
- prompt = f"Given the dataset: {data.to_dict(orient='records')}, answer the following: {query}"
77
 
78
- # Prepare the request payload
79
- payload = {
80
- "inputs": prompt,
81
- "max_tokens": 150, # Adjust the response length if necessary
82
- "temperature": 0.7 # Adjust temperature for randomness in response
83
- }
84
 
85
- # Send POST request to Groq API
86
- response = requests.post(api_url, headers=headers, data=json.dumps(payload))
87
-
88
- # Check for successful response
89
- if response.status_code == 200:
90
- response_json = response.json()
91
- st.write("Response from Groq Model:", response_json.get("generated_text", "No response"))
92
- else:
93
- st.error(f"Error querying Groq model: {response.status_code} - {response.text}")
94
  except Exception as e:
95
  st.error(f"Error in Groq processing: {e}")
96
 
@@ -109,10 +103,8 @@ if uploaded_file:
109
  st.write(data)
110
  elif "graph" in query.lower():
111
  generate_graph(data, query)
112
- elif "predict" in query.lower():
113
- st.write("Prediction functionality is in progress.")
114
  else:
115
- handle_query(data, query)
116
 
117
  # Footer
118
  footer = """
 
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
4
  import seaborn as sns
5
+ import os
6
+ from groq import Groq
7
+
8
+ # Check if openpyxl is installed
9
+ try:
10
+ import openpyxl
11
+ except ImportError:
12
+ st.error("openpyxl module is required to read Excel files. Attempting to install...")
13
+ os.system('pip install openpyxl')
14
+ st.success("openpyxl installed successfully. Please restart the app.")
15
 
16
  # Configure page
17
  st.set_page_config(page_title="Data Augmentation App", layout="wide")
 
34
  st.sidebar.title("Upload Your File")
35
  st.sidebar.markdown("Supported formats: CSV, Excel")
36
 
37
+ # Fetch Groq API key from Streamlit secrets
38
  groq_api_key = st.secrets.get("HUGGINGFACE_KEY") # Fetch Groq API key stored in secrets.json
39
  if not groq_api_key:
40
  st.error("Groq API key not found in secrets.")
41
  else:
42
  st.success("Groq API Key Loaded Successfully!")
43
 
44
+ # Initialize Groq client
45
+ client = Groq(api_key=groq_api_key)
46
+
47
  # Function to load data from file (CSV/Excel)
48
  def load_file(uploaded_file):
49
  """Load the uploaded file."""
 
73
  st.error(f"Error generating graph: {e}")
74
 
75
  # Function to handle user queries using the Groq model
76
+ def handle_query(query):
77
  """Handle user query using the Groq model."""
78
  try:
79
+ # Send user query to Groq model using Groq SDK
80
+ chat_completion = client.chat.completions.create(
81
+ messages=[{"role": "user", "content": query}],
82
+ model="llama3-8b-8192", # Model name as per your Groq model
83
+ )
 
 
 
 
84
 
85
+ # Display the response
86
+ st.write("Response from Groq Model:", chat_completion.choices[0].message.content)
 
 
 
 
87
 
 
 
 
 
 
 
 
 
 
88
  except Exception as e:
89
  st.error(f"Error in Groq processing: {e}")
90
 
 
103
  st.write(data)
104
  elif "graph" in query.lower():
105
  generate_graph(data, query)
 
 
106
  else:
107
+ handle_query(query) # Use Groq model for query processing
108
 
109
  # Footer
110
  footer = """