SHAMIL SHAHBAZ AWAN commited on
Commit
fe29d17
·
verified ·
1 Parent(s): b6ebb13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -15
app.py CHANGED
@@ -2,8 +2,8 @@ import streamlit as st
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
4
  import seaborn as sns
5
- from groqflow.groqmodel import GroqModel # Ensure this is the correct import for Groq model
6
- from groqflow.groqapi import GroqAPI # Adjust this based on your Groq library
7
 
8
  # Configure page
9
  st.set_page_config(page_title="Data Augmentation App", layout="wide")
@@ -27,18 +27,13 @@ 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") # Ensure this is stored in secrets.json
31
  if not groq_api_key:
32
  st.error("Groq API key not found in secrets.")
33
  else:
34
- try:
35
- # Initialize the Groq API or Groq model client
36
- groq_api = GroqAPI(api_key=groq_api_key)
37
- groq_model = GroqModel(model="llama-3.1-8b-instant", groq_api=groq_api) # Load the specific model
38
- st.success("Groq model initialized successfully!")
39
- except Exception as e:
40
- st.error(f"Error initializing Groq model: {e}")
41
 
 
42
  def load_file(uploaded_file):
43
  """Load the uploaded file."""
44
  if uploaded_file.name.endswith('.csv'):
@@ -49,6 +44,7 @@ def load_file(uploaded_file):
49
  st.error("Unsupported file format. Please upload a CSV or Excel file.")
50
  return None
51
 
 
52
  def generate_graph(data, query):
53
  """Generate a graph based on user query."""
54
  try:
@@ -65,15 +61,36 @@ def generate_graph(data, query):
65
  except Exception as e:
66
  st.error(f"Error generating graph: {e}")
67
 
 
68
  def handle_query(data, query):
69
  """Handle user query using the Groq model."""
70
  try:
71
- if not groq_model:
72
- st.error("Groq model is not initialized. Check for errors in setup.")
73
- return
 
 
 
 
 
74
  prompt = f"Given the dataset: {data.to_dict(orient='records')}, answer the following: {query}"
75
- response = groq_model.generate_text(prompt) # Use the Groq model's method for inference
76
- st.write("Response:", response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  except Exception as e:
78
  st.error(f"Error in Groq processing: {e}")
79
 
@@ -97,6 +114,7 @@ if uploaded_file:
97
  else:
98
  handle_query(data, query)
99
 
 
100
  footer = """
101
  <div style='text-align: left; padding: 10px;'>
102
  <footer>Created by: Shamil Shahbaz</footer>
 
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")
 
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."""
39
  if uploaded_file.name.endswith('.csv'):
 
44
  st.error("Unsupported file format. Please upload a CSV or Excel file.")
45
  return None
46
 
47
+ # Function to generate graph based on user query
48
  def generate_graph(data, query):
49
  """Generate a graph based on user query."""
50
  try:
 
61
  except Exception as e:
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
 
 
114
  else:
115
  handle_query(data, query)
116
 
117
+ # Footer
118
  footer = """
119
  <div style='text-align: left; padding: 10px;'>
120
  <footer>Created by: Shamil Shahbaz</footer>