Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from groq import Groq
|
4 |
+
import sqlite3
|
5 |
+
|
6 |
+
# Initialize Groq client with your API key
|
7 |
+
GROQ_API_KEY = "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"
|
8 |
+
client = Groq(api_key=GROQ_API_KEY)
|
9 |
+
|
10 |
+
# SQLite setup to store word history
|
11 |
+
def init_db():
|
12 |
+
conn = sqlite3.connect("word_history.db")
|
13 |
+
c = conn.cursor()
|
14 |
+
# Create table only if it doesn't already exist
|
15 |
+
c.execute('''CREATE TABLE IF NOT EXISTS history (
|
16 |
+
word TEXT PRIMARY KEY,
|
17 |
+
definition TEXT
|
18 |
+
)''')
|
19 |
+
conn.commit()
|
20 |
+
conn.close()
|
21 |
+
|
22 |
+
# Function to fetch word details from Groq API
|
23 |
+
def fetch_word_details(word):
|
24 |
+
try:
|
25 |
+
# Call Groq API with the word and get word details (meaning, synonyms, example sentences)
|
26 |
+
chat_completion = client.chat.completions.create(
|
27 |
+
messages=[{"role": "user", "content": f"Give me the meaning, synonyms, and example sentences for the word '{word}'"}],
|
28 |
+
model="llama3-8b-8192",
|
29 |
+
stream=False
|
30 |
+
)
|
31 |
+
response = chat_completion.choices[0].message.content
|
32 |
+
|
33 |
+
# Parsing the response to get meaningful details (example format)
|
34 |
+
return response
|
35 |
+
except Exception as e:
|
36 |
+
return f"Error fetching word details: {e}"
|
37 |
+
|
38 |
+
# Function to store word in history
|
39 |
+
def add_to_history(word, details):
|
40 |
+
conn = sqlite3.connect("word_history.db")
|
41 |
+
c = conn.cursor()
|
42 |
+
c.execute("INSERT OR REPLACE INTO history (word, definition) VALUES (?, ?)",
|
43 |
+
(word, details))
|
44 |
+
conn.commit()
|
45 |
+
conn.close()
|
46 |
+
|
47 |
+
# Function to fetch word history
|
48 |
+
def get_word_history():
|
49 |
+
conn = sqlite3.connect("word_history.db")
|
50 |
+
c = conn.cursor()
|
51 |
+
c.execute("SELECT word, definition FROM history")
|
52 |
+
history = c.fetchall()
|
53 |
+
conn.close()
|
54 |
+
return history
|
55 |
+
|
56 |
+
# Gradio UI
|
57 |
+
def real_time_dictionary_app(word_input):
|
58 |
+
# Fetch word details from Groq's API
|
59 |
+
details = fetch_word_details(word_input)
|
60 |
+
|
61 |
+
# Store this word in history
|
62 |
+
add_to_history(word_input, details)
|
63 |
+
|
64 |
+
# Show history of previously searched words
|
65 |
+
history = get_word_history()
|
66 |
+
|
67 |
+
# Prepare the output to display
|
68 |
+
output = f"Results for '{word_input}':\n{details}\n\nWord History:\n"
|
69 |
+
for word, definition in history:
|
70 |
+
output += f"{word}: {definition}\n"
|
71 |
+
|
72 |
+
return output
|
73 |
+
|
74 |
+
# Initialize the database
|
75 |
+
init_db()
|
76 |
+
|
77 |
+
# Gradio interface
|
78 |
+
title = "Real-Time Dictionary App"
|
79 |
+
gr.Interface(
|
80 |
+
fn=real_time_dictionary_app,
|
81 |
+
inputs=gr.Textbox(label="Enter a word"),
|
82 |
+
outputs=gr.Textbox(label="Results"),
|
83 |
+
live=True,
|
84 |
+
title=title,
|
85 |
+
description="Search for word meanings, synonyms, and example sentences."
|
86 |
+
).launch()
|