zainulabedin949 commited on
Commit
d48f72d
·
verified ·
1 Parent(s): f900b38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -39
app.py CHANGED
@@ -2,19 +2,28 @@ import os
2
  import pandas as pd
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
- import gradio as gr
6
- from groq import Groq
7
 
8
- GROQ_API_KEY = "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"
 
 
9
 
10
- client = Groq(api_key= GROQ_API_KEY)
 
11
 
12
- def analyze_file(uploaded_file):
 
 
 
13
  # Load the file into a pandas DataFrame
14
  if uploaded_file.name.endswith('.csv'):
15
- df = pd.read_csv(uploaded_file.name)
16
  elif uploaded_file.name.endswith('.xlsx'):
17
- df = pd.read_excel(uploaded_file.name)
 
 
 
18
 
19
  # Perform basic data analysis
20
  if 'Gain (dB)' in df.columns and 'Frequency (GHz)' in df.columns:
@@ -22,15 +31,19 @@ def analyze_file(uploaded_file):
22
  median_gain = df['Gain (dB)'].median()
23
  std_dev_gain = df['Gain (dB)'].std()
24
 
25
- # Plot the data
 
 
 
 
26
  fig, ax = plt.subplots()
27
  ax.plot(df['Frequency (GHz)'], df['Gain (dB)'], label='Gain (dB)', color='blue')
28
  ax.set_xlabel('Frequency (GHz)')
29
  ax.set_ylabel('Gain (dB)')
30
  ax.set_title('Gain vs Frequency')
31
  ax.legend()
32
- plt.close(fig) # Close to avoid inline display
33
-
34
  # Send summary to Groq API for analysis
35
  data_summary = f"""
36
  The dataset contains simulation results for antennas. The frequency range is from 1 GHz to 10 GHz.
@@ -38,34 +51,25 @@ def analyze_file(uploaded_file):
38
  - Efficiency is consistently above 90%, with the highest reaching 99%.
39
  """
40
 
41
- chat_completion = client.chat.completions.create(
42
- messages=[{
43
- "role": "user",
44
- "content": f"Analyze this data summary: {data_summary}",
45
- }],
46
- model="llama-3.3-70b-versatile", # Ensure this model is supported by Groq
47
- )
48
-
49
- groq_analysis = chat_completion.choices[0].message.content
50
-
51
- return mean_gain, median_gain, std_dev_gain, fig, groq_analysis
52
- else:
53
- return "Required columns 'Gain (dB)' or 'Frequency (GHz)' not found in the dataset."
54
 
55
- # Create the Gradio interface
56
- iface = gr.Interface(
57
- fn=analyze_file,
58
- inputs=gr.inputs.File(label="Upload CSV or Excel File"),
59
- outputs=[
60
- gr.outputs.Textbox(label="Mean Gain"),
61
- gr.outputs.Textbox(label="Median Gain"),
62
- gr.outputs.Textbox(label="Standard Deviation of Gain"),
63
- gr.outputs.Image(label="Plot"),
64
- gr.outputs.Textbox(label="Groq's Analysis")
65
- ],
66
- title="Data Analysis and Visualization with Groq API",
67
- description="Upload a CSV or Excel file to analyze the antenna data and get insights."
68
- )
69
 
70
- # Launch the interface
71
- iface.launch()
 
 
 
 
 
 
 
 
 
 
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"
11
 
12
+ # Streamlit UI Setup
13
+ st.title("Data Analysis and Visualization with Groq API")
14
 
15
+ # File upload section
16
+ uploaded_file = st.file_uploader("Upload CSV or Excel File", type=["csv", "xlsx"])
17
+
18
+ if uploaded_file is not None:
19
  # Load the file into a pandas DataFrame
20
  if uploaded_file.name.endswith('.csv'):
21
+ df = pd.read_csv(uploaded_file)
22
  elif uploaded_file.name.endswith('.xlsx'):
23
+ df = pd.read_excel(uploaded_file)
24
+
25
+ # Display Data Preview
26
+ st.write("Data Preview", df.head())
27
 
28
  # Perform basic data analysis
29
  if 'Gain (dB)' in df.columns and 'Frequency (GHz)' in df.columns:
 
31
  median_gain = df['Gain (dB)'].median()
32
  std_dev_gain = df['Gain (dB)'].std()
33
 
34
+ st.write(f"Mean Gain: {mean_gain}")
35
+ st.write(f"Median Gain: {median_gain}")
36
+ st.write(f"Standard Deviation of Gain: {std_dev_gain}")
37
+
38
+ # Plotting the data
39
  fig, ax = plt.subplots()
40
  ax.plot(df['Frequency (GHz)'], df['Gain (dB)'], label='Gain (dB)', color='blue')
41
  ax.set_xlabel('Frequency (GHz)')
42
  ax.set_ylabel('Gain (dB)')
43
  ax.set_title('Gain vs Frequency')
44
  ax.legend()
45
+ st.pyplot(fig)
46
+
47
  # Send summary to Groq API for analysis
48
  data_summary = f"""
49
  The dataset contains simulation results for antennas. The frequency range is from 1 GHz to 10 GHz.
 
51
  - Efficiency is consistently above 90%, with the highest reaching 99%.
52
  """
53
 
54
+ # Setup headers and payload for the API request
55
+ headers = {
56
+ "Authorization": f"Bearer {API_KEY}",
57
+ "Content-Type": "application/json"
58
+ }
 
 
 
 
 
 
 
 
59
 
60
+ payload = {
61
+ "messages": [{"role": "user", "content": data_summary}],
62
+ "model": "llama-3.3-70b-versatile" # Ensure this model is supported by Groq
63
+ }
 
 
 
 
 
 
 
 
 
 
64
 
65
+ # Send request to Groq API
66
+ response = requests.post(API_URL, json=payload, headers=headers)
67
+
68
+ if response.status_code == 200:
69
+ groq_analysis = response.json()["choices"][0]["message"]["content"]
70
+ st.write("Groq's Analysis:")
71
+ st.write(groq_analysis)
72
+ else:
73
+ st.write(f"Error: {response.status_code}, {response.text}")
74
+ else:
75
+ st.write("Required columns 'Gain (dB)' or 'Frequency (GHz)' not found in the dataset.")