import os import gradio as gr from groq import Groq import sqlite3 # Initialize Groq client with your API key GROQ_API_KEY = "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" client = Groq(api_key=GROQ_API_KEY) # SQLite setup to store word history def init_db(): conn = sqlite3.connect("word_history.db") c = conn.cursor() # Create table only if it doesn't already exist c.execute('''CREATE TABLE IF NOT EXISTS history ( word TEXT PRIMARY KEY, definition TEXT )''') conn.commit() conn.close() # Function to fetch word details from Groq API def fetch_word_details(word): try: # Call Groq API with the word and get word details (meaning, synonyms, example sentences) chat_completion = client.chat.completions.create( messages=[{"role": "user", "content": f"Give me the meaning, synonyms, and example sentences for the word '{word}'"}], model="llama3-8b-8192", stream=False ) response = chat_completion.choices[0].message.content # Parsing the response to get meaningful details (example format) return response except Exception as e: return f"Error fetching word details: {e}" # Function to store word in history def add_to_history(word, details): conn = sqlite3.connect("word_history.db") c = conn.cursor() c.execute("INSERT OR REPLACE INTO history (word, definition) VALUES (?, ?)", (word, details)) conn.commit() conn.close() # Function to fetch word history def get_word_history(): conn = sqlite3.connect("word_history.db") c = conn.cursor() c.execute("SELECT word, definition FROM history") history = c.fetchall() conn.close() return history # Gradio UI def real_time_dictionary_app(word_input): # Fetch word details from Groq's API details = fetch_word_details(word_input) # Store this word in history add_to_history(word_input, details) # Show history of previously searched words history = get_word_history() # Prepare the output to display output = f"Results for '{word_input}':\n{details}\n\nWord History:\n" for word, definition in history: output += f"{word}: {definition}\n" return output # Initialize the database init_db() # Gradio interface title = "Real-Time Dictionary App" gr.Interface( fn=real_time_dictionary_app, inputs=gr.Textbox(label="Enter a word"), outputs=gr.Textbox(label="Results"), live=True, title=title, description="Search for word meanings, synonyms, and example sentences." ).launch()