File size: 11,288 Bytes
98346d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

#  Main App using Gradio 



#import all the libraries and functions 
import gradio as gr
from src.features.translation import translate_text
from src.features.summarization import summarize_text
from src.features.keyword_extraction import extract_keywords
from src.features.email_writer import write_email, write_cover_letter
from src.features.pdf_qa import ask_pdf, load_pdf
from src.graphs.chat_graph import ChatSession



# === CHAT SESSION (persistent across turns) ===
chat_session = ChatSession()


# === TRANSLATION TAB ===
def translation_tab():
    with gr.Tab("🌐 Translation"):
        gr.Markdown("## Language Translation\nTranslate text between 100+ languages.")
        with gr.Row():
            with gr.Column():
                translate_input = gr.Textbox(
                    label="Text to Translate",
                    placeholder="Enter text here...",
                    lines=5
                )
                with gr.Row():
                    source_lang = gr.Dropdown(
                        label="Source Language",
                        choices=["English", "French", "Spanish", "German",
                                 "Italian", "Portuguese", "Chinese", "Japanese",
                                 "Arabic", "Hindi", "Korean", "Russian"],
                        value="English"
                    )
                    target_lang = gr.Dropdown(
                        label="Target Language",
                        choices=["English", "French", "Spanish", "German",
                                 "Italian", "Portuguese", "Chinese", "Japanese",
                                 "Arabic", "Hindi", "Korean", "Russian"],
                        value="French"
                    )
                translate_btn = gr.Button("Translate 🌐", variant="primary")
            with gr.Column():
                translate_output = gr.Textbox(
                    label="Translation",
                    lines=5
                )

        translate_btn.click(
            fn=translate_text,
            inputs=[translate_input, source_lang, target_lang],
            outputs=translate_output
        )



#Summerization 
def summarization_tab():
    with gr.Tab("πŸ“ Summarization"):
        gr.Markdown("## Text Summarization\nSummarize long documents and articles.")
        with gr.Row():
            with gr.Column():
                summary_input = gr.Textbox(
                    label="Text to Summarize",
                    placeholder="Paste your text here...",
                    lines=10
                )
                summary_mode = gr.Radio(
                    label="Summary Mode",
                    choices=["concise", "detailed"],
                    value="concise"
                )
                summary_btn = gr.Button("Summarize πŸ“", variant="primary")
            with gr.Column():
                summary_output = gr.Textbox(
                    label="Summary",
                    lines=10
                )

        summary_btn.click(
            fn=summarize_text,
            inputs=[summary_input, summary_mode],
            outputs=summary_output
        )

# === KEYWORD EXTRACTION TAB ===
def keyword_tab():
    with gr.Tab("πŸ” Keywords"):
        gr.Markdown("## Keyword Extraction\nExtract key topics from any text.")
        with gr.Row():
            with gr.Column():
                keyword_input = gr.Textbox(
                    label="Text",
                    placeholder="Paste your text here...",
                    lines=8
                )
                num_keywords = gr.Slider(
                    label="Number of Keywords",
                    minimum=5,
                    maximum=20,
                    value=10,
                    step=1
                )
                keyword_btn = gr.Button("Extract Keywords πŸ”", variant="primary")
            with gr.Column():
                keyword_output = gr.Textbox(
                    label="Keywords",
                    lines=8
                )

        keyword_btn.click(
            fn=extract_keywords,
            inputs=[keyword_input, num_keywords],
            outputs=keyword_output
        )



# === EMAIL WRITER TAB ===
def email_tab():
    with gr.Tab("βœ‰οΈ Email Writer"):
        gr.Markdown("## Email & Cover Letter Writer")
        with gr.Tabs():
            with gr.Tab("Email"):
                with gr.Row():
                    with gr.Column():
                        email_type = gr.Dropdown(
                            label="Email Type",
                            choices=["follow-up", "introduction", "apology",
                                     "request", "thank you", "complaint",
                                     "proposal", "invitation"],
                            value="follow-up"
                        )
                        email_context = gr.Textbox(
                            label="Context / Purpose",
                            placeholder="Describe the purpose of the email...",
                            lines=4
                        )
                        email_tone = gr.Dropdown(
                            label="Tone",
                            choices=["professional", "friendly", "formal",
                                     "casual", "urgent"],
                            value="professional"
                        )
                        with gr.Row():
                            email_recipient = gr.Textbox(
                                label="Recipient Name",
                                placeholder="Hiring Manager"
                            )
                            email_sender = gr.Textbox(
                                label="Your Name",
                                placeholder="Shashank"
                            )
                        email_btn = gr.Button("Write Email βœ‰οΈ", variant="primary")
                    with gr.Column():
                        email_output = gr.Textbox(label="Generated Email", lines=15)

                email_btn.click(
                    fn=write_email,
                    inputs=[email_type, email_context, email_tone,
                            email_recipient, email_sender],
                    outputs=email_output
                )

            with gr.Tab("Cover Letter"):
                with gr.Row():
                    with gr.Column():
                        cl_name     = gr.Textbox(label="Your Name", placeholder="Shashank Hegde")
                        cl_job      = gr.Textbox(label="Job Title", placeholder="ML Engineer")
                        cl_company  = gr.Textbox(label="Company", placeholder="Google")
                        cl_skills   = gr.Textbox(label="Key Skills", placeholder="Python, ML, LangChain...", lines=3)
                        cl_exp      = gr.Textbox(label="Experience Summary", placeholder="3 years in ML...", lines=3)
                        cl_btn      = gr.Button("Write Cover Letter πŸ“„", variant="primary")
                    with gr.Column():
                        cl_output = gr.Textbox(label="Cover Letter", lines=15)

                cl_btn.click(
                    fn=write_cover_letter,
                    inputs=[cl_job, cl_company, cl_skills, cl_exp, cl_name],
                    outputs=cl_output
                )


# === CHAT TAB ===
def chat_tab():
    with gr.Tab("πŸ’¬ Chat Assistant"):
        gr.Markdown("## AI Chat Assistant\nAsk me anything!")
        chatbot = gr.Chatbot(height=400)
        with gr.Row():
            chat_input = gr.Textbox(
                label="Message",
                placeholder="Type your message here...",
                scale=4
            )
            chat_btn = gr.Button("Send πŸ’¬", variant="primary", scale=1)
        clear_btn = gr.Button("Clear Conversation πŸ—‘οΈ")
        
        def respond(message, history):
            if not message.strip():
                return "", history
            response = chat_session.chat(message)
            history.append((message, response))
            return "", history
        
        def clear():
            chat_session.clear_history()
            return []

        chat_btn.click(
            fn=respond,
            inputs=[chat_input, chatbot],
            outputs=[chat_input, chatbot]
        )

        clear_btn.click(
            fn=clear,
            inputs=[],
            outputs=[chatbot]
        )

        chat_input.submit(
            fn=respond,
            inputs=[chat_input, chatbot],
            outputs=[chat_input, chatbot]
        )

        clear_btn.click(fn=clear, outputs=chatbot)


# === PDF Q&A TAB ===
def pdf_tab():
    pdf_text_state = gr.State("")

    with gr.Tab("πŸ“„ PDF Q&A"):
        gr.Markdown("## PDF Q&A\nUpload a PDF and ask questions about it.")
        with gr.Row():
            with gr.Column():
                pdf_upload = gr.File(
                    label="Upload PDF",
                    file_types=[".pdf"]
                )
                pdf_status = gr.Textbox(label="Status", interactive=False)
                pdf_question = gr.Textbox(
                    label="Ask a Question",
                    placeholder="What is this document about?",
                    lines=3
                )
                pdf_btn = gr.Button("Ask πŸ”", variant="primary")
            with gr.Column():
                pdf_output = gr.Textbox(label="Answer", lines=15)

        def load_pdf_file(file):
            if file is None:
                return "", "⚠️ No file uploaded."
            text = load_pdf(file.name)
            return text, f"βœ… PDF loaded: {len(text.split())} words"

        def answer_question(question, pdf_text):
            if not pdf_text:
                return "⚠️ Please upload a PDF first."
            if not question.strip():
                return "⚠️ Please enter a question."
            import tempfile, os
            with tempfile.NamedTemporaryFile(mode='w', suffix='.txt',
                                             delete=False) as f:
                f.write(pdf_text)
                tmp_path = f.name

            from src.features.pdf_qa import build_pdf_qa_graph
            graph = build_pdf_qa_graph()
            result = graph.invoke({
                "pdf_text": pdf_text,
                "question": question,
                "context": "",
                "answer": ""
            })
            os.unlink(tmp_path)
            return result["answer"]

        pdf_upload.change(
            fn=load_pdf_file,
            inputs=pdf_upload,
            outputs=[pdf_text_state, pdf_status]
        )
        pdf_btn.click(
            fn=answer_question,
            inputs=[pdf_question, pdf_text_state],
            outputs=pdf_output
        )

# === MAIN APP ===
def create_app():
    with gr.Blocks(
        title="πŸ€– GenAI Toolkit",
        theme=gr.themes.Soft()
    ) as app:
        gr.Markdown("""
        # πŸ€– GenAI Toolkit
        ### Powered by LangChain + LangGraph + Groq
        """)

        translation_tab()
        summarization_tab()
        keyword_tab()
        email_tab()
        chat_tab()
        pdf_tab()

    return app

if __name__ == "__main__":
    app = create_app()
    app.launch(server_name="0.0.0.0", server_port=7860)