Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- .gitattributes +1 -0
- app.py +149 -0
- index_alone.faiss +3 -0
- requirements.txt +4 -0
- themes_2.py +53 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
index_alone.faiss filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from sentence_transformers import SentenceTransformer
|
3 |
+
import datasets
|
4 |
+
import time
|
5 |
+
import faiss
|
6 |
+
|
7 |
+
# Impor tema custom dari themes.py
|
8 |
+
from themes_2 import ClassicMinimalTheme
|
9 |
+
|
10 |
+
|
11 |
+
# Inisialisasi dataset dan model hanya sekali di Gradio
|
12 |
+
def initialize():
|
13 |
+
dataset = datasets.load_dataset('A-Roucher/english_historical_quotes', download_mode="force_redownload")['train']
|
14 |
+
all_authors = list(set(dataset['author']))
|
15 |
+
model_name = "BAAI/bge-small-en-v1.5"
|
16 |
+
encoder = SentenceTransformer(model_name)
|
17 |
+
index = faiss.read_index('index_alone.faiss')
|
18 |
+
|
19 |
+
return dataset, encoder, index, all_authors
|
20 |
+
|
21 |
+
# Fungsi untuk melakukan pencarian kutipan berdasarkan query
|
22 |
+
def search(query, dataset, encoder, index, progress=gr.Progress()):
|
23 |
+
start = time.time()
|
24 |
+
|
25 |
+
if len(query.strip()) == 0:
|
26 |
+
return "Silakan masukkan ide atau kata kunci." # Tidak ada pencarian jika query kosong
|
27 |
+
|
28 |
+
# Memulai progres dengan 0%
|
29 |
+
progress(0, desc="Memulai proses...")
|
30 |
+
|
31 |
+
# Encode query menjadi embedding
|
32 |
+
progress(0.2, desc="Proses encoding...")
|
33 |
+
query_embedding = encoder.encode([query])
|
34 |
+
time.sleep(1) # Simulasi proses lambat
|
35 |
+
|
36 |
+
# Cari kutipan yang paling mirip menggunakan faiss
|
37 |
+
progress(0.5, desc="Menemukan kutipan yang mirip...")
|
38 |
+
_, samples = index.search(query_embedding, k=10)
|
39 |
+
time.sleep(1)
|
40 |
+
|
41 |
+
quotes = dataset.select(samples[0])
|
42 |
+
|
43 |
+
result = "\n\n"
|
44 |
+
for i in progress.tqdm(range(len(quotes)), desc="Menyusun hasil..."):
|
45 |
+
time.sleep(0.25) # Simulasi penundaan saat menyusun hasil
|
46 |
+
# Menggunakan HTML untuk mengatur ukuran font pada quote dan tebal pada author
|
47 |
+
result += f"## โญ {quotes['author'][i]}\n> {quotes['quote'][i]}\n----\n"
|
48 |
+
# result += f"<b>{quotes['author'][i]}</b><br><span style='font-size: 20px;'>{quotes['quote'][i]}</span></hr>"
|
49 |
+
|
50 |
+
delay = "%.3f" % (time.time() - start)
|
51 |
+
|
52 |
+
# Proses selesai 100%
|
53 |
+
progress(1.0, desc="Selesai!")
|
54 |
+
|
55 |
+
return f"_Waktu komputasi: **{delay} detik**_{result}"
|
56 |
+
|
57 |
+
# Fungsi untuk memulai pencarian dengan progress tracking
|
58 |
+
def run_search(query):
|
59 |
+
dataset, encoder, index, _ = initialize() # Ambil state inisialisasi (dataset, encoder, index)
|
60 |
+
return search(query, dataset, encoder, index) # Mengembalikan hasil akhir sebagai string
|
61 |
+
|
62 |
+
# CSS Kustom untuk mempercantik tampilan
|
63 |
+
css = """
|
64 |
+
#header {
|
65 |
+
text-align: center;
|
66 |
+
color: #FFFFFF;
|
67 |
+
background: linear-gradient(90deg, #FF5733, #C70039);
|
68 |
+
padding: 20px;
|
69 |
+
border-radius: 10px;
|
70 |
+
}
|
71 |
+
|
72 |
+
#input-section, #output-section {
|
73 |
+
margin: 10px auto;
|
74 |
+
max-width: 700px;
|
75 |
+
background-color: #F0F0F0;
|
76 |
+
border-radius: 10px;
|
77 |
+
padding: 20px;
|
78 |
+
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
|
79 |
+
}
|
80 |
+
|
81 |
+
#footer {
|
82 |
+
text-align: center;
|
83 |
+
margin-top: 30px;
|
84 |
+
color: #888;
|
85 |
+
}
|
86 |
+
|
87 |
+
#submit-button {
|
88 |
+
background-color: #FF5733;
|
89 |
+
color: #FFF;
|
90 |
+
font-weight: bold;
|
91 |
+
padding: 10px;
|
92 |
+
border-radius: 10px;
|
93 |
+
cursor: pointer;
|
94 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
95 |
+
}
|
96 |
+
|
97 |
+
body {
|
98 |
+
background-color: #F8F9F9;
|
99 |
+
}
|
100 |
+
|
101 |
+
.note {
|
102 |
+
background-color: #ddffdd;
|
103 |
+
border: 1px solid #ddd;
|
104 |
+
padding: 10px;
|
105 |
+
margin: 10px 0;
|
106 |
+
border-radius: 5px;
|
107 |
+
font-family: Arial, sans-serif;
|
108 |
+
}
|
109 |
+
"""
|
110 |
+
|
111 |
+
# -----------------
|
112 |
+
# Antarmuka Gradio
|
113 |
+
# -----------------
|
114 |
+
with gr.Blocks(css=css, theme=ClassicMinimalTheme()) as Apps:
|
115 |
+
|
116 |
+
# Tambahkan banner
|
117 |
+
gr.HTML("""
|
118 |
+
<div style='text-align: center;'>
|
119 |
+
<img src='https://i.ibb.co.com/x6XxZC9/banner02.jpg' alt='Banner' style='width: 100%; height: auto;'/>
|
120 |
+
</div>
|
121 |
+
""")
|
122 |
+
|
123 |
+
# Ganti judul
|
124 |
+
gr.Markdown("<h1 style='text-align: center;'>Siapa yang memiliki ide serupa dengan Anda?</br>๐ Kami bantu Anda mencarinya dengan AI.. </h1>")
|
125 |
+
|
126 |
+
|
127 |
+
# Input Section
|
128 |
+
with gr.Row(elem_id="input-section"):
|
129 |
+
text_input = gr.Textbox(label="Ketik ide Anda di sini:", placeholder="Money .", lines=2)
|
130 |
+
submit_button = gr.Button("๐ Cari kutipan!", elem_id="submit-button")
|
131 |
+
|
132 |
+
# Output Section
|
133 |
+
with gr.Row(elem_id="output-section"):
|
134 |
+
output = gr.Markdown()
|
135 |
+
|
136 |
+
# Menghubungkan tombol pencarian dengan fungsi search
|
137 |
+
submit_button.click(run_search, inputs=[text_input], outputs=[output])
|
138 |
+
|
139 |
+
# Tambahkan footer di bagian bawah
|
140 |
+
gr.HTML("""
|
141 |
+
<footer id="footer">
|
142 |
+
Transfer Energi Semesta Digital ยฉ 2024 __drat. | ๐ฎ๐ฉ Untuk Indonesia Jaya!
|
143 |
+
</footer>
|
144 |
+
""")
|
145 |
+
|
146 |
+
|
147 |
+
# Meluncurkan aplikasi
|
148 |
+
if __name__ == "__main__":
|
149 |
+
Apps.queue(api_open=False).launch(show_api=False)
|
index_alone.faiss
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8fc54f449bbdeab574ff823646dd728e6b460f296e46e8bcfcedb28d6d3bf6a5
|
3 |
+
size 36897837
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
datasets==2.14.6
|
2 |
+
faiss-cpu==1.7.3
|
3 |
+
sentence_transformers==2.2.2
|
4 |
+
streamlit==1.28.1
|
themes_2.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
from typing import Iterable
|
3 |
+
from gradio.themes.base import Base
|
4 |
+
from gradio.themes.utils import colors, fonts, sizes
|
5 |
+
|
6 |
+
class ClassicMinimalTheme(Base):
|
7 |
+
def __init__(
|
8 |
+
self,
|
9 |
+
*,
|
10 |
+
primary_hue: colors.Color | str = colors.slate,
|
11 |
+
secondary_hue: colors.Color | str = colors.gray,
|
12 |
+
neutral_hue: colors.Color | str = colors.stone,
|
13 |
+
spacing_size: sizes.Size | str = sizes.spacing_md,
|
14 |
+
radius_size: sizes.Size | str = sizes.radius_none, # Tidak ada radius untuk kesan minimalis
|
15 |
+
text_size: sizes.Size | str = sizes.text_md,
|
16 |
+
font: fonts.Font
|
17 |
+
| str
|
18 |
+
| Iterable[fonts.Font | str] = (
|
19 |
+
fonts.GoogleFont("Merriweather"), # Font klasik, serupa dengan buku-buku lama
|
20 |
+
"serif",
|
21 |
+
),
|
22 |
+
font_mono: fonts.Font
|
23 |
+
| str
|
24 |
+
| Iterable[fonts.Font | str] = (
|
25 |
+
fonts.GoogleFont("Courier Prime"), # Font monospace untuk teks yang terlihat klasik
|
26 |
+
"monospace",
|
27 |
+
),
|
28 |
+
):
|
29 |
+
super().__init__(
|
30 |
+
primary_hue=primary_hue,
|
31 |
+
secondary_hue=secondary_hue,
|
32 |
+
neutral_hue=neutral_hue,
|
33 |
+
spacing_size=spacing_size,
|
34 |
+
radius_size=radius_size,
|
35 |
+
text_size=text_size,
|
36 |
+
font=font,
|
37 |
+
font_mono=font_mono,
|
38 |
+
)
|
39 |
+
super().set(
|
40 |
+
body_background_fill="linear-gradient(to bottom, #f5f5dc, #d2b48c)", # Warna latar belakang beige ke coklat muda untuk kesan kuno
|
41 |
+
body_background_fill_dark="linear-gradient(to bottom, #3e2723, #1b1b1b)", # Warna lebih gelap untuk dark mode
|
42 |
+
button_primary_background_fill="linear-gradient(90deg, #8b4513, #a0522d)", # Warna coklat tua untuk tombol utama
|
43 |
+
button_primary_background_fill_hover="linear-gradient(90deg, #a0522d, #cd853f)", # Warna coklat lebih terang saat hover
|
44 |
+
button_primary_text_color="white",
|
45 |
+
button_primary_background_fill_dark="linear-gradient(90deg, #3e2723, #5d4037)", # Warna coklat tua untuk dark mode
|
46 |
+
slider_color="*secondary_300",
|
47 |
+
slider_color_dark="*secondary_600",
|
48 |
+
block_title_text_weight="700", # Teks judul tebal
|
49 |
+
block_border_width="2px", # Garis perbatasan yang sederhana
|
50 |
+
block_shadow="none", # Tidak ada bayangan untuk gaya minimalis
|
51 |
+
button_shadow="none", # Tombol tanpa bayangan, memberi kesan bersih
|
52 |
+
button_large_padding="16px", # Ukuran padding tombol lebih kecil dan sederhana
|
53 |
+
)
|