File size: 2,656 Bytes
fbb5966
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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()