darsoarafa commited on
Commit
2ec4176
·
verified ·
1 Parent(s): 897a6c6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +179 -0
app.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import random
4
+ import string
5
+ import datetime
6
+
7
+ # Daftar produk dan kategori
8
+ categories = {
9
+ "Kategori A": ["Produk A1", "Produk A2", "Produk A3", "Produk A4", "Produk A5", "Produk A6"],
10
+ "Kategori B": ["Produk B1", "Produk B2", "Produk B3", "Produk B4", "Produk B5", "Produk B6"],
11
+ "Kategori C": ["Produk C1", "Produk C2", "Produk C3", "Produk C4", "Produk C5", "Produk C6"],
12
+ "Kategori D": ["Produk D1", "Produk D2", "Produk D3", "Produk D4", "Produk D5", "Produk D6"]
13
+ }
14
+
15
+ # Inisialisasi model pengenalan suara
16
+ speech_recognizer = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
17
+
18
+ # Fungsi untuk mengonversi suara ke teks
19
+ def speech_to_text(audio):
20
+ if audio is None:
21
+ return ""
22
+ result = speech_recognizer(audio)
23
+ return result["text"]
24
+
25
+ # Fungsi untuk membuat dokumen LaTeX
26
+ def create_latex(data, filename):
27
+ latex_content = r"""
28
+ \documentclass[a4paper,12pt]{article}
29
+ \usepackage[utf8]{inputenc}
30
+ \usepackage[T1]{fontenc}
31
+ \usepackage{lmodern}
32
+ \usepackage{geometry}
33
+ \geometry{margin=1in}
34
+ \usepackage{booktabs}
35
+ \usepackage{fancyhdr}
36
+ \pagestyle{fancy}
37
+ \fancyhf{}
38
+ \fancyhead[C]{Dokumen Penawaran \\ PT. Contoh Perusahaan \\ Jl. Contoh Alamat No. 123}
39
+ \fancyfoot[C]{\thepage}
40
+
41
+ \begin{document}
42
+
43
+ \begin{center}
44
+ \textbf{\LARGE Dokumen Penawaran} \\
45
+ \vspace{0.5cm}
46
+ PT. Contoh Perusahaan \\
47
+ Jl. Contoh Alamat No. 123 \\
48
+ \vspace{0.5cm}
49
+ \end{center}
50
+
51
+ Kepada Yth. \\
52
+ \textbf{""" + data['nama_prospek'] + r"""} \\
53
+ """ + data['alamat_prospek'] + r""" \\
54
+ Jenis Prospek: """ + data['jenis_prospek'] + r""" \\
55
+ \vspace{0.5cm}
56
+
57
+ Tanggal: """ + data['tanggal'] + r""" \\
58
+ \vspace{0.5cm}
59
+
60
+ \textbf{Daftar Produk yang Ditawarkan:} \\
61
+ \begin{tabular}{|l|c|r|}
62
+ \hline
63
+ \textbf{Nama Produk} & \textbf{Jumlah} & \textbf{Harga (Rp)} \\
64
+ \hline
65
+ """ + "\n ".join([f"{p['nama']} & {p['jumlah']} & {p['harga']:,}" for p in data['produk']]) + r""" \\
66
+ \hline
67
+ \end{tabular}
68
+ \vspace{0.5cm}
69
+
70
+ Diskon: """ + (f"{data['diskon']}\%" if 'diskon' in data else "Tidak ada") + r""" \\
71
+ \vspace{0.5cm}
72
+
73
+ """ + (r"""\textbf{Syarat dan Ketentuan:} \\
74
+ """ + data['syarat'] + r""" \\
75
+ \vspace{0.5cm}""" if 'syarat' in data else r"Syarat dan Ketentuan: Tidak ada \\ \vspace{0.5cm}") + r"""
76
+
77
+ Hormat kami, \\
78
+ \vspace{0.5cm}
79
+ PT. Contoh Perusahaan
80
+
81
+ \end{document}
82
+ """
83
+ with open(filename, "w") as f:
84
+ f.write(latex_content)
85
+ return filename
86
+
87
+ # Fungsi untuk memproses input dan membuat penawaran
88
+ def buat_penawaran(nama_prospek, alamat_prospek, jenis_prospek, produk_dipilih, jumlah_produk, harga_produk, diskon, tanggal, syarat, audio_input):
89
+ # Proses input suara jika ada
90
+ if audio_input:
91
+ audio_text = speech_to_text(audio_input).lower()
92
+ if "nama" in audio_text and not nama_prospek:
93
+ nama_prospek = audio_text.split("nama")[-1].strip()
94
+ if "alamat" in audio_text and not alamat_prospek:
95
+ alamat_prospek = audio_text.split("alamat")[-1].strip()
96
+ # Logika serupa dapat ditambahkan untuk field lain
97
+
98
+ data = {
99
+ 'nama_prospek': nama_prospek if nama_prospek else "Prospek Tanpa Nama",
100
+ 'alamat_prospek': alamat_prospek if alamat_prospek else "Alamat Tidak Diketahui",
101
+ 'jenis_prospek': jenis_prospek if jenis_prospek else "Individu",
102
+ 'tanggal': tanggal if tanggal else datetime.date.today().strftime("%Y-%m-%d"),
103
+ 'produk': []
104
+ }
105
+
106
+ # Proses produk
107
+ if produk_dipilih and jumlah_produk and harga_produk:
108
+ jumlah_list = [int(x.strip()) for x in jumlah_produk.split(",") if x.strip()]
109
+ harga_list = [int(x.strip()) for x in harga_produk.split(",") if x.strip()]
110
+ for i, p in enumerate(produk_dipilih):
111
+ j = jumlah_list[i] if i < len(jumlah_list) else 1
112
+ h = harga_list[i] if i < len(harga_list) else 100000
113
+ data['produk'].append({'nama': p, 'jumlah': j, 'harga': h})
114
+ else:
115
+ data['produk'].append({'nama': "Produk Contoh", 'jumlah': 1, 'harga': 100000})
116
+
117
+ if diskon:
118
+ data['diskon'] = diskon
119
+ if syarat:
120
+ data['syarat'] = syarat
121
+
122
+ # Validasi data
123
+ missing_data = []
124
+ if not nama_prospek:
125
+ missing_data.append("Nama Prospek")
126
+ if not alamat_prospek:
127
+ missing_data.append("Alamat Prospek")
128
+ if not jenis_prospek:
129
+ missing_data.append("Jenis Prospek")
130
+ if not tanggal:
131
+ missing_data.append("Tanggal")
132
+ if not produk_dipilih or not jumlah_produk or not harga_produk:
133
+ missing_data.append("Produk, Jumlah, atau Harga")
134
+
135
+ warning = ""
136
+ if missing_data:
137
+ warning = "Peringatan: Data berikut kurang dan telah diisi dengan asumsi: " + ", ".join(missing_data) + ". Silakan lengkapi data jika perlu."
138
+
139
+ # Buat file LaTeX
140
+ filename = ''.join(random.choices(string.ascii_letters + string.digits, k=10)) + ".tex"
141
+ latex_file = create_latex(data, filename)
142
+ return latex_file, warning
143
+
144
+ # Interface Gradio
145
+ with gr.Blocks() as demo:
146
+ gr.Markdown("# Aplikasi Pembuatan Dokumen Penawaran")
147
+
148
+ with gr.Row():
149
+ nama_prospek = gr.Textbox(label="Nama Prospek")
150
+ alamat_prospek = gr.Textbox(label="Alamat Prospek")
151
+ jenis_prospek = gr.Dropdown(choices=["Individu", "Perusahaan", "Kafe"], label="Jenis Prospek")
152
+
153
+ with gr.Row():
154
+ tanggal = gr.Textbox(label="Tanggal Penawaran (YYYY-MM-DD)")
155
+
156
+ with gr.Row():
157
+ produk_dipilih = gr.CheckboxGroup(choices=[p for cat in categories.values() for p in cat], label="Pilih Produk")
158
+ jumlah_produk = gr.Textbox(label="Jumlah Produk (pisahkan dengan koma)")
159
+ harga_produk = gr.Textbox(label="Harga per Produk (Rp, pisahkan dengan koma)")
160
+
161
+ with gr.Row():
162
+ diskon = gr.Textbox(label="Diskon (%)")
163
+ syarat = gr.Textbox(label="Syarat dan Ketentuan")
164
+
165
+ with gr.Row():
166
+ audio_input = gr.Audio(source="microphone", label="Input Suara (opsional)")
167
+
168
+ submit_button = gr.Button("Buat Penawaran")
169
+
170
+ output_file = gr.File(label="Download Dokumen Penawaran (LaTeX, akan dirender sebagai PDF)")
171
+ warning_text = gr.Textbox(label="Peringatan")
172
+
173
+ submit_button.click(
174
+ buat_penawaran,
175
+ inputs=[nama_prospek, alamat_prospek, jenis_prospek, produk_dipilih, jumlah_produk, harga_produk, diskon, tanggal, syarat, audio_input],
176
+ outputs=[output_file, warning_text]
177
+ )
178
+
179
+ demo.launch()