Here’s a Python app that demonstrates the power of ChatGPT-4o by integrating AI-driven natural language processing with real-time data visualization. This app allows users to enter any topic, and ChatGPT-4o generates a summary, key insights, and even a sentiment analysis. The results are displayed dynamically using Matplotlib and Tkinter.
import tkinter as tk
from tkinter import scrolledtext
import openai
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from textblob import TextBlob
import threading
# Replace with your actual OpenAI API key
openai.api_key = "YOUR_OPENAI_API_KEY"
def analyze_text(topic):
try:
response = openai.ChatCompletion.create(
model="gpt-4o",
messages=[{"role": "system", "content": "You are an expert analyst."},
{"role": "user", "content": f"Give me insights about {topic}."}]
)
text = response["choices"][0]["message"]["content"]
sentiment = TextBlob(text).sentiment.polarity
generate_wordcloud(text)
display_result(text, sentiment)
except Exception as e:
display_result(f"Error: {str(e)}", 0)
def generate_wordcloud(text):
wordcloud = WordCloud(width=800, height=400, background_color='white').generate(text)
plt.figure(figsize=(8, 4))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.title("Word Cloud of Generated Insights")
plt.show()
def display_result(text, sentiment):
result_text.config(state=tk.NORMAL)
result_text.delete(1.0, tk.END)
result_text.insert(tk.INSERT, text)
result_text.config(state=tk.DISABLED)
sentiment_label.config(text=f"Sentiment Score: {'Positive' if sentiment > 0 else 'Negative' if sentiment < 0 else 'Neutral'} ({sentiment:.2f})")
def on_submit():
topic = input_text.get()
if topic:
threading.Thread(target=analyze_text, args=(topic,), daemon=True).start()
# GUI Setup
root = tk.Tk()
root.title("ChatGPT-4o AI Insights Analyzer")
root.geometry("600x500")
input_label = tk.Label(root, text="Enter a Topic:", font=("Arial", 12))
input_label.pack(pady=5)
input_text = tk.Entry(root, width=50, font=("Arial", 12))
input_text.pack(pady=5)
submit_btn = tk.Button(root, text="Analyze", font=("Arial", 12), command=on_submit)
submit_btn.pack(pady=5)
result_text = scrolledtext.ScrolledText(root, wrap=tk.WORD, width=70, height=10, font=("Arial", 10))
result_text.pack(pady=10)
result_text.config(state=tk.DISABLED)
sentiment_label = tk.Label(root, text="Sentiment Score: ", font=("Arial", 12))
sentiment_label.pack(pady=5)
root.mainloop()
# Requirements.txt
"""
openai
matplotlib
wordcloud
textblob
pillow
tkinter
"""
# Running Instructions
"""
1. Install dependencies:
pip install -r requirements.txt
2. Replace 'YOUR_OPENAI_API_KEY' with a valid OpenAI API key.
3. Run the script:
python script_name.py
"""
