intelligent-data-roadmap / gradio_app.py
fultoncannon's picture
Upload gradio_app.py
6a4b763 verified
import gradio as gr
import pandas as pd
import openai
import io
def setup_openai(api_key):
openai.api_key = api_key
try:
# Test API key with a simple request
response = openai.Completion.create(
model="gpt-3.5-turbo",
prompt="Tell me a joke",
max_tokens=50
)
return response.choices[0].text.strip(), None
except Exception as e:
return None, f"Error with API key: {str(e)}"
def analyze_data_with_llm(df):
# Convert the dataframe to a string format to send to the LLM
data_str = df.head(10).to_string() # For efficiency, limit to first 10 rows
# Prepare prompt for the LLM
prompt = f"Given the following dataset:\n{data_str}\n\nWhat AI/ML features or techniques could benefit from this data?"
# Query the LLM for suggestions
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
max_tokens=150,
temperature=0.7
)
return response.choices[0].text.strip()
def process_files(api_key, *files):
if not api_key:
return "API Key is required to proceed.", None
# Set up OpenAI API
joke, error = setup_openai(api_key)
if error:
return error, None
outputs = []
outputs.append(f"API Key Test Joke: {joke}\n")
for file in files:
if file is None:
continue
output = f"πŸ“ File: {file.name}\n"
try:
if file.name.endswith(".xlsx"):
df = pd.read_excel(io.BytesIO(file.read()))
else:
df = pd.read_csv(io.StringIO(file.read().decode('utf-8')))
output += f"Shape: {df.shape}\n\n"
output += "Preview:\n" + df.head().to_string() + "\n\n"
# Analyze the data and get suggestions from the LLM
suggestions = analyze_data_with_llm(df)
output += "AI/ML Feature Suggestions:\n" + suggestions + "\n\n"
except Exception as e:
output += f"Error processing file: {str(e)}\n\n"
outputs.append(output)
return "\n".join(outputs), None
# Create Gradio interface
with gr.Blocks(title="πŸ“Š CSV/XLSX Data Inspector with AI/ML Feature Suggestions") as iface:
gr.Markdown("# πŸ“Š CSV/XLSX Data Inspector with AI/ML Feature Suggestions")
api_key_input = gr.Textbox(
label="Enter your OpenAI API key",
type="password",
placeholder="sk-..."
)
file_input = gr.File(
label="Upload CSV or XLSX files",
file_types=[".csv", ".xlsx"],
file_count="multiple"
)
output_text = gr.Textbox(label="Results")
submit_btn = gr.Button("Analyze Files")
submit_btn.click(
fn=process_files,
inputs=[api_key_input, file_input],
outputs=[output_text]
)
iface.launch()