zainulabedin949 commited on
Commit
49ac35b
·
verified ·
1 Parent(s): d821384

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -44
app.py CHANGED
@@ -7,56 +7,75 @@ 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
  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:
21
- mean_gain = df['Gain (dB)'].mean()
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.
37
- - The antenna's gain increases from 5 dB to 30 dB as frequency increases.
38
- - Efficiency is consistently above 90%, with the highest reaching 99%.
39
- """
40
-
41
- headers = {
42
- "Authorization": f"Bearer {API_KEY}",
43
- "Content-Type": "application/json"
44
- }
45
 
46
- payload = {
47
- "messages": [{"role": "user", "content": data_summary}],
48
- "model": "llama-3.3-70b-versatile" # Ensure this model is supported by Groq
49
- }
50
 
51
- response = requests.post(API_URL, json=payload, headers=headers)
52
- if response.status_code == 200:
53
- groq_analysis = response.json()["choices"][0]["message"]["content"]
 
 
 
 
 
54
  else:
55
- groq_analysis = f"Error: {response.status_code}, {response.text}"
56
-
57
- return mean_gain, median_gain, std_dev_gain, fig, groq_analysis
58
- else:
59
- return "Required columns 'Gain (dB)' or 'Frequency (GHz)' not found in the dataset."
 
60
 
61
  # Create the Gradio interface using the new Gradio 3.x syntax
62
  iface = gr.Interface(
 
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
  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
+ # Display the column names and first few rows for debugging
23
+ print("Columns in the uploaded file:", df.columns)
24
+ print("Preview of the uploaded data:", df.head())
25
+
26
+ # Check if required columns are present
27
+ if 'Gain (dB)' in df.columns and 'Frequency (GHz)' in df.columns:
28
+ # Perform basic data analysis
29
+ mean_gain = df['Gain (dB)'].mean()
30
+ median_gain = df['Gain (dB)'].median()
31
+ std_dev_gain = df['Gain (dB)'].std()
32
+
33
+ # Display analysis results
34
+ print(f"Mean Gain: {mean_gain}")
35
+ print(f"Median Gain: {median_gain}")
36
+ print(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
+ plt.close(fig) # Close to avoid inline display
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.
50
+ - The antenna's gain increases from 5 dB to 30 dB as frequency increases.
51
+ - Efficiency is consistently above 90%, with the highest reaching 99%.
52
+ """
53
+
54
+ headers = {
55
+ "Authorization": f"Bearer {API_KEY}",
56
+ "Content-Type": "application/json"
57
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
+ payload = {
60
+ "messages": [{"role": "user", "content": data_summary}],
61
+ "model": "llama-3.3-70b-versatile" # Ensure this model is supported by Groq
62
+ }
63
 
64
+ # Send the request to Groq API
65
+ response = requests.post(API_URL, json=payload, headers=headers)
66
+ if response.status_code == 200:
67
+ groq_analysis = response.json()["choices"][0]["message"]["content"]
68
+ else:
69
+ groq_analysis = f"Error: {response.status_code}, {response.text}"
70
+
71
+ return mean_gain, median_gain, std_dev_gain, fig, groq_analysis
72
  else:
73
+ return "Error: Required columns 'Gain (dB)' or 'Frequency (GHz)' not found in the dataset."
74
+
75
+ except Exception as e:
76
+ # Return error message if something goes wrong
77
+ return f"An error occurred: {str(e)}"
78
+
79
 
80
  # Create the Gradio interface using the new Gradio 3.x syntax
81
  iface = gr.Interface(