Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
# Groq API Setup
|
| 9 |
+
API_KEY = "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"
|
| 10 |
+
API_URL = "https://api.groq.com/openai/v1/chat/completions" # Updated API URL
|
| 11 |
+
|
| 12 |
+
def analyze_file(uploaded_file):
|
| 13 |
+
try:
|
| 14 |
+
# Load the file into a pandas DataFrame
|
| 15 |
+
if uploaded_file.name.endswith('.csv'):
|
| 16 |
+
df = pd.read_csv(uploaded_file.name)
|
| 17 |
+
elif uploaded_file.name.endswith('.xlsx'):
|
| 18 |
+
df = pd.read_excel(uploaded_file.name)
|
| 19 |
+
else:
|
| 20 |
+
return "Error: The uploaded file is neither CSV nor Excel."
|
| 21 |
+
|
| 22 |
+
# Clean up the column names by stripping any leading/trailing spaces
|
| 23 |
+
df.columns = df.columns.str.strip()
|
| 24 |
+
|
| 25 |
+
# Display the column names and first few rows for debugging
|
| 26 |
+
st.write("Columns in the uploaded file:", df.columns)
|
| 27 |
+
st.write("Preview of the uploaded data:", df.head())
|
| 28 |
+
|
| 29 |
+
# Check if required columns are present
|
| 30 |
+
if 'Gain (dB)' in df.columns and 'Frequency (GHz)' in df.columns:
|
| 31 |
+
# Perform basic data analysis
|
| 32 |
+
mean_gain = df['Gain (dB)'].mean()
|
| 33 |
+
median_gain = df['Gain (dB)'].median()
|
| 34 |
+
std_dev_gain = df['Gain (dB)'].std()
|
| 35 |
+
|
| 36 |
+
# Display analysis results
|
| 37 |
+
st.write(f"Mean Gain: {mean_gain}")
|
| 38 |
+
st.write(f"Median Gain: {median_gain}")
|
| 39 |
+
st.write(f"Standard Deviation of Gain: {std_dev_gain}")
|
| 40 |
+
|
| 41 |
+
# Plotting the data
|
| 42 |
+
fig, ax = plt.subplots()
|
| 43 |
+
ax.plot(df['Frequency (GHz)'], df['Gain (dB)'], label='Gain (dB)', color='blue')
|
| 44 |
+
ax.set_xlabel('Frequency (GHz)')
|
| 45 |
+
ax.set_ylabel('Gain (dB)')
|
| 46 |
+
ax.set_title('Gain vs Frequency')
|
| 47 |
+
ax.legend()
|
| 48 |
+
st.pyplot(fig)
|
| 49 |
+
|
| 50 |
+
# Send summary to Groq API for analysis
|
| 51 |
+
data_summary = f"""
|
| 52 |
+
The dataset contains simulation results for antennas. The frequency range is from 1 GHz to 10 GHz.
|
| 53 |
+
- The antenna's gain increases from 5 dB to 30 dB as frequency increases.
|
| 54 |
+
- Efficiency is consistently above 90%, with the highest reaching 99%.
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
headers = {
|
| 58 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 59 |
+
"Content-Type": "application/json"
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
payload = {
|
| 63 |
+
"messages": [{"role": "user", "content": data_summary}],
|
| 64 |
+
"model": "llama-3.3-70b-versatile" # Ensure this model is supported by Groq
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
# Send the request to Groq API
|
| 68 |
+
response = requests.post(API_URL, json=payload, headers=headers)
|
| 69 |
+
if response.status_code == 200:
|
| 70 |
+
groq_analysis = response.json()["choices"][0]["message"]["content"]
|
| 71 |
+
else:
|
| 72 |
+
groq_analysis = f"Error: {response.status_code}, {response.text}"
|
| 73 |
+
|
| 74 |
+
return mean_gain, median_gain, std_dev_gain, fig, groq_analysis
|
| 75 |
+
else:
|
| 76 |
+
return "Error: Required columns 'Gain (dB)' or 'Frequency (GHz)' not found in the dataset."
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
# Return error message if something goes wrong
|
| 80 |
+
return f"An error occurred: {str(e)}"
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# Streamlit Interface
|
| 84 |
+
st.title("Data Analysis and Visualization with Groq API")
|
| 85 |
+
st.write("Upload a CSV or Excel file to analyze the antenna data and get insights.")
|
| 86 |
+
|
| 87 |
+
# File upload
|
| 88 |
+
uploaded_file = st.file_uploader("Choose a file", type=["xlsx", "csv"])
|
| 89 |
+
|
| 90 |
+
if uploaded_file is not None:
|
| 91 |
+
results = analyze_file(uploaded_file)
|
| 92 |
+
|
| 93 |
+
if isinstance(results, tuple): # If it's a valid result (tuple)
|
| 94 |
+
mean_gain, median_gain, std_dev_gain, fig, groq_analysis = results
|
| 95 |
+
|
| 96 |
+
st.write(f"Mean Gain: {mean_gain}")
|
| 97 |
+
st.write(f"Median Gain: {median_gain}")
|
| 98 |
+
st.write(f"Standard Deviation of Gain: {std_dev_gain}")
|
| 99 |
+
|
| 100 |
+
st.pyplot(fig) # Show plot
|
| 101 |
+
|
| 102 |
+
st.write("Groq's Analysis:")
|
| 103 |
+
st.write(groq_analysis)
|
| 104 |
+
else:
|
| 105 |
+
st.write(results) # Error message
|