Spaces:
Sleeping
Sleeping
Commit ·
98346d0
0
Parent(s):
Initial GenAI Toolkit deployment
Browse files- Dockerfile +16 -0
- README.md +8 -0
- requirements.txt +0 -0
- src/__init__.py +0 -0
- src/__pycache__/__init__.cpython-311.pyc +0 -0
- src/__pycache__/app.cpython-311.pyc +0 -0
- src/app.py +282 -0
- src/app1.py +318 -0
- src/features/Readme.md +28 -0
- src/features/__pycache__/email_writer.cpython-311.pyc +0 -0
- src/features/__pycache__/keyword_extraction.cpython-311.pyc +0 -0
- src/features/__pycache__/pdf_qa.cpython-311.pyc +0 -0
- src/features/__pycache__/summarization.cpython-311.pyc +0 -0
- src/features/__pycache__/summarization.cpython-314.pyc +0 -0
- src/features/__pycache__/translation.cpython-311.pyc +0 -0
- src/features/email_writer.py +112 -0
- src/features/keyword_extraction.py +43 -0
- src/features/pdf_qa.py +114 -0
- src/features/summarization.py +93 -0
- src/features/translation.py +43 -0
- src/graphs/__init__.py +1 -0
- src/graphs/__pycache__/__init__.cpython-311.pyc +0 -0
- src/graphs/__pycache__/chat_graph.cpython-311.pyc +0 -0
- src/graphs/chat_graph.py +93 -0
- src/requirements.txt +10 -0
- src/utils/__init__.py +1 -0
- src/utils/__pycache__/__init__.cpython-311.pyc +0 -0
- src/utils/__pycache__/llm.cpython-311.pyc +0 -0
- src/utils/__pycache__/vector_store.cpython-311.pyc +0 -0
- src/utils/llm.py +34 -0
- src/utils/vector_store.py +31 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --upgrade pip && pip install -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY src/ ./src/
|
| 9 |
+
|
| 10 |
+
ENV PYTHONUNBUFFERED=1
|
| 11 |
+
ENV PYTHONPATH=/app
|
| 12 |
+
ENV GROQ_API_KEY=""
|
| 13 |
+
|
| 14 |
+
EXPOSE 7860
|
| 15 |
+
|
| 16 |
+
CMD ["python", "-m", "src.app"]
|
README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: GenAI Toolkit
|
| 3 |
+
emoji: 🤖
|
| 4 |
+
colorFrom: teal
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
requirements.txt
ADDED
|
Binary file (4.28 kB). View file
|
|
|
src/__init__.py
ADDED
|
File without changes
|
src/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (152 Bytes). View file
|
|
|
src/__pycache__/app.cpython-311.pyc
ADDED
|
Binary file (26.1 kB). View file
|
|
|
src/app.py
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from src.features.translation import translate_text
|
| 3 |
+
from src.features.summarization import summarize_text
|
| 4 |
+
from src.features.keyword_extraction import extract_keywords
|
| 5 |
+
from src.features.email_writer import write_email, write_cover_letter
|
| 6 |
+
from src.features.pdf_qa import ask_pdf, load_pdf, build_pdf_qa_graph
|
| 7 |
+
from src.graphs.chat_graph import ChatSession
|
| 8 |
+
|
| 9 |
+
chat_session = ChatSession()
|
| 10 |
+
|
| 11 |
+
LANGUAGES = [
|
| 12 |
+
"English", "French", "Spanish", "German", "Italian",
|
| 13 |
+
"Portuguese", "Chinese", "Japanese", "Arabic", "Hindi",
|
| 14 |
+
"Korean", "Russian", "Dutch", "Swedish", "Turkish",
|
| 15 |
+
"Polish", "Ukrainian", "Vietnamese", "Thai", "Indonesian"
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
custom_css = """
|
| 19 |
+
:root {
|
| 20 |
+
--primary: #6C5CE7;
|
| 21 |
+
--primary-light: #a29bfe;
|
| 22 |
+
--surface: #ffffff;
|
| 23 |
+
--surface-2: #f8f7ff;
|
| 24 |
+
--border: #e8e5ff;
|
| 25 |
+
--text: #2d2d3a;
|
| 26 |
+
--text-muted: #8b8b9e;
|
| 27 |
+
--success: #00b894;
|
| 28 |
+
--radius: 16px;
|
| 29 |
+
--radius-sm: 10px;
|
| 30 |
+
--shadow: 0 4px 24px rgba(108,92,231,0.10);
|
| 31 |
+
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
body, .gradio-container {
|
| 35 |
+
background: linear-gradient(135deg, #f8f7ff 0%, #f0eeff 100%) !important;
|
| 36 |
+
font-family: 'Inter', 'Segoe UI', sans-serif !important;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
.gradio-container { max-width: 1200px !important; margin: 0 auto !important; padding: 2rem !important; }
|
| 40 |
+
|
| 41 |
+
/* Force teal on primary buttons */
|
| 42 |
+
button.primary,
|
| 43 |
+
.primary-btn {
|
| 44 |
+
background: linear-gradient(135deg, #0D9488, #0c0d0d) !important;
|
| 45 |
+
border: none !important;
|
| 46 |
+
color: white !important;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
/* Header */
|
| 50 |
+
.app-header {
|
| 51 |
+
background: linear-gradient(135deg, #0D9488, #14B8A6);
|
| 52 |
+
border-radius: var(--radius);
|
| 53 |
+
padding: 2.5rem 2rem;
|
| 54 |
+
margin-bottom: 2rem;
|
| 55 |
+
text-align: center;
|
| 56 |
+
color: white;
|
| 57 |
+
box-shadow: var(--shadow);
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
/* Tabs */
|
| 61 |
+
.tab-nav { background: white !important; border-radius: var(--radius) !important; padding: 6px !important; border: 1px solid var(--border) !important; box-shadow: var(--shadow) !important; gap: 4px !important; }
|
| 62 |
+
.tab-nav button { border-radius: var(--radius-sm) !important; border: none !important; color: var(--text-muted) !important; font-weight: 500 !important; padding: 10px 20px !important; transition: all 0.2s !important; }
|
| 63 |
+
.tab-nav button.selected { background: linear-gradient(135deg, #6C5CE7, #a29bfe) !important; color: white !important; box-shadow: 0 4px 12px rgba(108,92,231,0.3) !important; }
|
| 64 |
+
|
| 65 |
+
/* Cards */
|
| 66 |
+
.feature-card { background: white !important; border-radius: var(--radius) !important; border: 1px solid var(--border) !important; padding: 1.5rem !important; box-shadow: var(--shadow) !important; }
|
| 67 |
+
|
| 68 |
+
/* Inputs */
|
| 69 |
+
textarea, input[type=text], input[type=number] { border-radius: var(--radius-sm) !important; border: 1.5px solid var(--border) !important; background: var(--surface-2) !important; font-size: 14px !important; transition: border-color 0.2s !important; }
|
| 70 |
+
textarea:focus, input:focus { border-color: var(--primary) !important; box-shadow: 0 0 0 3px rgba(108,92,231,0.15) !important; }
|
| 71 |
+
|
| 72 |
+
/* Buttons */
|
| 73 |
+
.primary-btn { background: linear-gradient(135deg, #6C5CE7, #a29bfe) !important; border: none !important; border-radius: var(--radius-sm) !important; color: white !important; font-weight: 600 !important; padding: 12px 28px !important; font-size: 14px !important; box-shadow: 0 4px 12px rgba(108,92,231,0.3) !important; transition: all 0.2s !important; }
|
| 74 |
+
.primary-btn:hover { transform: translateY(-1px) !important; box-shadow: 0 6px 20px rgba(108,92,231,0.4) !important; }
|
| 75 |
+
.secondary-btn { background: white !important; border: 1.5px solid var(--border) !important; border-radius: var(--radius-sm) !important; color: var(--primary) !important; font-weight: 500 !important; }
|
| 76 |
+
|
| 77 |
+
/* Dropdowns */
|
| 78 |
+
.wrap { border-radius: var(--radius-sm) !important; border: 1.5px solid var(--border) !important; background: var(--surface-2) !important; }
|
| 79 |
+
|
| 80 |
+
/* Labels */
|
| 81 |
+
label span { font-weight: 600 !important; color: var(--text) !important; font-size: 13px !important; letter-spacing: 0.3px !important; }
|
| 82 |
+
|
| 83 |
+
/* Chat */
|
| 84 |
+
.chatbot { border-radius: var(--radius) !important; border: 1px solid var(--border) !important; box-shadow: var(--shadow) !important; }
|
| 85 |
+
.message.user { background: linear-gradient(135deg, #6C5CE7, #a29bfe) !important; color: white !important; border-radius: 18px 18px 4px 18px !important; }
|
| 86 |
+
.message.bot { background: white !important; border: 1px solid var(--border) !important; border-radius: 18px 18px 18px 4px !important; }
|
| 87 |
+
|
| 88 |
+
/* Section headers */
|
| 89 |
+
.section-title { font-size: 22px !important; font-weight: 700 !important; color: var(--text) !important; margin-bottom: 4px !important; }
|
| 90 |
+
.section-subtitle { font-size: 14px !important; color: var(--text-muted) !important; margin-bottom: 1.5rem !important; }
|
| 91 |
+
|
| 92 |
+
/* Stat badges */
|
| 93 |
+
.badge { display: inline-block; background: var(--surface-2); border: 1px solid var(--border); border-radius: 20px; padding: 4px 14px; font-size: 12px; color: var(--primary); font-weight: 600; margin: 4px; }
|
| 94 |
+
|
| 95 |
+
/* File upload */
|
| 96 |
+
.upload-btn { border-radius: var(--radius-sm) !important; border: 2px dashed var(--primary-light) !important; background: var(--surface-2) !important; }
|
| 97 |
+
|
| 98 |
+
/* Slider */
|
| 99 |
+
input[type=range] { accent-color: var(--primary) !important; }
|
| 100 |
+
|
| 101 |
+
/* Output boxes */
|
| 102 |
+
.output-box { background: var(--surface-2) !important; border: 1.5px solid var(--border) !important; border-radius: var(--radius-sm) !important; }
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
def create_app():
|
| 106 |
+
with gr.Blocks(title="GenAI Toolkit",
|
| 107 |
+
|
| 108 |
+
css=custom_css,
|
| 109 |
+
theme=gr.themes.Base(
|
| 110 |
+
primary_hue=gr.themes.colors.blue,
|
| 111 |
+
neutral_hue=gr.themes.colors.slate,
|
| 112 |
+
font=[gr.themes.GoogleFont("Inter"), "sans-serif"]
|
| 113 |
+
)
|
| 114 |
+
) as app:
|
| 115 |
+
|
| 116 |
+
gr.HTML("""
|
| 117 |
+
<div class="app-header">
|
| 118 |
+
<div style="font-size:40px;margin-bottom:10px">🤖</div>
|
| 119 |
+
<h1 style="font-size:32px;font-weight:800;margin:0 0 8px">GenAI Toolkit</h1>
|
| 120 |
+
<p style="font-size:16px;opacity:0.9;margin:0">Powered by LangChain · LangGraph · Groq LLaMA 3</p>
|
| 121 |
+
<div style="margin-top:16px">
|
| 122 |
+
<span class="badge">🌐 Translation</span>
|
| 123 |
+
<span class="badge">📝 Summarization</span>
|
| 124 |
+
<span class="badge">💬 Chat</span>
|
| 125 |
+
<span class="badge">📄 PDF Q&A</span>
|
| 126 |
+
<span class="badge">✉️ Email Writer</span>
|
| 127 |
+
<span class="badge">🔍 Keywords</span>
|
| 128 |
+
</div>
|
| 129 |
+
</div>
|
| 130 |
+
""")
|
| 131 |
+
|
| 132 |
+
with gr.Tabs(elem_classes="tab-nav"):
|
| 133 |
+
|
| 134 |
+
# ── TRANSLATION ──────────────────────────────────────────
|
| 135 |
+
with gr.Tab("🌐 Translation"):
|
| 136 |
+
gr.HTML('<p class="section-title">Language Translation</p><p class="section-subtitle">Translate text instantly across 20+ languages using LLaMA 3</p>')
|
| 137 |
+
with gr.Row(equal_height=True):
|
| 138 |
+
with gr.Column(scale=1, elem_classes="feature-card"):
|
| 139 |
+
t_input = gr.Textbox(label="Source Text", placeholder="Type or paste text to translate...", lines=8)
|
| 140 |
+
with gr.Row():
|
| 141 |
+
t_src = gr.Dropdown(label="From", choices=LANGUAGES, value="English")
|
| 142 |
+
t_tgt = gr.Dropdown(label="To", choices=LANGUAGES, value="French")
|
| 143 |
+
t_btn = gr.Button("Translate ➜", variant="primary", elem_classes="primary-btn")
|
| 144 |
+
with gr.Column(scale=1, elem_classes="feature-card"):
|
| 145 |
+
t_out = gr.Textbox(label="Translation", lines=8, interactive=False, elem_classes="output-box")
|
| 146 |
+
gr.HTML('<p style="font-size:12px;color:#8b8b9e;margin-top:8px">⚡ Powered by LLaMA 3.1 · Fast & accurate</p>')
|
| 147 |
+
t_btn.click(fn=translate_text, inputs=[t_input, t_src, t_tgt], outputs=t_out)
|
| 148 |
+
|
| 149 |
+
# ── SUMMARIZATION ─────────────────────────────────────────
|
| 150 |
+
with gr.Tab("📝 Summarize"):
|
| 151 |
+
gr.HTML('<p class="section-title">Text Summarization</p><p class="section-subtitle">Condense long articles, reports, and documents into clear summaries</p>')
|
| 152 |
+
with gr.Row(equal_height=True):
|
| 153 |
+
with gr.Column(scale=1, elem_classes="feature-card"):
|
| 154 |
+
s_input = gr.Textbox(label="Text to Summarize", placeholder="Paste your article, report, or any long text here...", lines=12)
|
| 155 |
+
with gr.Row():
|
| 156 |
+
s_mode = gr.Radio(label="Summary Style", choices=["concise", "detailed"], value="concise")
|
| 157 |
+
s_btn = gr.Button("Summarize ✦", variant="primary", elem_classes="primary-btn")
|
| 158 |
+
with gr.Column(scale=1, elem_classes="feature-card"):
|
| 159 |
+
s_out = gr.Textbox(label="Summary", lines=12, interactive=False, elem_classes="output-box")
|
| 160 |
+
gr.HTML('<p style="font-size:12px;color:#8b8b9e;margin-top:8px">📊 Supports long documents via map-reduce chunking</p>')
|
| 161 |
+
s_btn.click(fn=summarize_text, inputs=[s_input, s_mode], outputs=s_out)
|
| 162 |
+
|
| 163 |
+
# ── KEYWORDS ─────────────────────────────────────────────
|
| 164 |
+
with gr.Tab("🔍 Keywords"):
|
| 165 |
+
gr.HTML('<p class="section-title">Keyword Extraction</p><p class="section-subtitle">Identify the most important topics and phrases in any text</p>')
|
| 166 |
+
with gr.Row(equal_height=True):
|
| 167 |
+
with gr.Column(scale=1, elem_classes="feature-card"):
|
| 168 |
+
k_input = gr.Textbox(label="Input Text", placeholder="Paste any text to extract key topics...", lines=10)
|
| 169 |
+
k_num = gr.Slider(label="Number of Keywords", minimum=5, maximum=20, value=10, step=1)
|
| 170 |
+
k_btn = gr.Button("Extract Keywords 🔍", variant="primary", elem_classes="primary-btn")
|
| 171 |
+
with gr.Column(scale=1, elem_classes="feature-card"):
|
| 172 |
+
k_out = gr.Textbox(label="Extracted Keywords", lines=10, interactive=False, elem_classes="output-box")
|
| 173 |
+
gr.HTML('<p style="font-size:12px;color:#8b8b9e;margin-top:8px">🏷️ Returns keywords with context and relevance</p>')
|
| 174 |
+
k_btn.click(fn=extract_keywords, inputs=[k_input, k_num], outputs=k_out)
|
| 175 |
+
|
| 176 |
+
# ── EMAIL WRITER ──────────────────────────────────────────
|
| 177 |
+
with gr.Tab("✉️ Email Writer"):
|
| 178 |
+
gr.HTML('<p class="section-title">Email & Cover Letter Writer</p><p class="section-subtitle">Generate professional emails and cover letters instantly</p>')
|
| 179 |
+
with gr.Tabs():
|
| 180 |
+
with gr.Tab("📧 Email"):
|
| 181 |
+
with gr.Row(equal_height=True):
|
| 182 |
+
with gr.Column(scale=1, elem_classes="feature-card"):
|
| 183 |
+
e_type = gr.Dropdown(label="Email Type", choices=["follow-up","introduction","apology","request","thank you","complaint","proposal","invitation"], value="follow-up")
|
| 184 |
+
e_ctx = gr.Textbox(label="Purpose / Context", placeholder="Describe the goal of this email...", lines=4)
|
| 185 |
+
e_tone = gr.Dropdown(label="Tone", choices=["professional","friendly","formal","casual","urgent"], value="professional")
|
| 186 |
+
with gr.Row():
|
| 187 |
+
e_to = gr.Textbox(label="Recipient", placeholder="Hiring Manager")
|
| 188 |
+
e_from = gr.Textbox(label="Your Name", placeholder="Shashank")
|
| 189 |
+
e_btn = gr.Button("Generate Email ✉️", variant="primary", elem_classes="primary-btn")
|
| 190 |
+
with gr.Column(scale=1, elem_classes="feature-card"):
|
| 191 |
+
e_out = gr.Textbox(label="Generated Email", lines=18, interactive=False, elem_classes="output-box")
|
| 192 |
+
e_btn.click(fn=write_email, inputs=[e_type, e_ctx, e_tone, e_to, e_from], outputs=e_out)
|
| 193 |
+
|
| 194 |
+
with gr.Tab("📄 Cover Letter"):
|
| 195 |
+
with gr.Row(equal_height=True):
|
| 196 |
+
with gr.Column(scale=1, elem_classes="feature-card"):
|
| 197 |
+
cl_name = gr.Textbox(label="Your Name", placeholder="Shashank Hegde")
|
| 198 |
+
cl_job = gr.Textbox(label="Job Title Applying For", placeholder="ML Engineer")
|
| 199 |
+
cl_co = gr.Textbox(label="Company Name", placeholder="Google DeepMind")
|
| 200 |
+
cl_sk = gr.Textbox(label="Key Skills", placeholder="Python, LangChain, MLflow, XGBoost...", lines=3)
|
| 201 |
+
cl_exp = gr.Textbox(label="Experience Summary", placeholder="3 years building ML pipelines...", lines=3)
|
| 202 |
+
cl_btn = gr.Button("Generate Cover Letter 📄", variant="primary", elem_classes="primary-btn")
|
| 203 |
+
with gr.Column(scale=1, elem_classes="feature-card"):
|
| 204 |
+
cl_out = gr.Textbox(label="Cover Letter", lines=18, interactive=False, elem_classes="output-box")
|
| 205 |
+
cl_btn.click(fn=write_cover_letter, inputs=[cl_job, cl_co, cl_sk, cl_exp, cl_name], outputs=cl_out)
|
| 206 |
+
|
| 207 |
+
# ── CHAT ──────────────────────────────────────────────────
|
| 208 |
+
with gr.Tab("💬 Chat"):
|
| 209 |
+
gr.HTML('<p class="section-title">AI Chat Assistant</p><p class="section-subtitle">Multi-turn conversations powered by LangGraph + LLaMA 3.3 70B</p>')
|
| 210 |
+
with gr.Column(elem_classes="feature-card"):
|
| 211 |
+
chatbot = gr.Chatbot(height=450, label="", elem_classes="chatbot", show_label=False)
|
| 212 |
+
with gr.Row():
|
| 213 |
+
c_input = gr.Textbox(label="", placeholder="Ask me anything...", scale=5, container=False)
|
| 214 |
+
c_btn = gr.Button("Send ➜", variant="primary", scale=1, elem_classes="primary-btn")
|
| 215 |
+
with gr.Row():
|
| 216 |
+
c_clear = gr.Button("🗑️ Clear Conversation", elem_classes="secondary-btn")
|
| 217 |
+
gr.HTML('<p style="font-size:12px;color:#8b8b9e;padding-top:10px">🧠 Maintains full conversation context via LangGraph state</p>')
|
| 218 |
+
|
| 219 |
+
def respond(message, history):
|
| 220 |
+
if not message.strip():
|
| 221 |
+
return "", history
|
| 222 |
+
response = chat_session.chat(message)
|
| 223 |
+
history.append((message, response))
|
| 224 |
+
return "", history
|
| 225 |
+
|
| 226 |
+
def clear():
|
| 227 |
+
chat_session.clear_history()
|
| 228 |
+
return []
|
| 229 |
+
|
| 230 |
+
c_btn.click(fn=respond, inputs=[c_input, chatbot], outputs=[c_input, chatbot])
|
| 231 |
+
c_input.submit(fn=respond, inputs=[c_input, chatbot], outputs=[c_input, chatbot])
|
| 232 |
+
c_clear.click(fn=clear, outputs=chatbot)
|
| 233 |
+
|
| 234 |
+
# ── PDF Q&A ───────────────────────────────────────────────
|
| 235 |
+
with gr.Tab("📄 PDF Q&A"):
|
| 236 |
+
gr.HTML('<p class="section-title">PDF Question & Answer</p><p class="section-subtitle">Upload any PDF and ask questions — powered by RAG + LangGraph</p>')
|
| 237 |
+
pdf_state = gr.State("")
|
| 238 |
+
with gr.Row(equal_height=True):
|
| 239 |
+
with gr.Column(scale=1, elem_classes="feature-card"):
|
| 240 |
+
p_file = gr.File(label="Upload PDF", file_types=[".pdf"], elem_classes="upload-btn")
|
| 241 |
+
p_status = gr.Textbox(label="Status", interactive=False, lines=1)
|
| 242 |
+
p_q = gr.Textbox(label="Your Question", placeholder="What is the main topic of this document?", lines=4)
|
| 243 |
+
p_btn = gr.Button("Ask Question 🔍", variant="primary", elem_classes="primary-btn")
|
| 244 |
+
gr.HTML('<p style="font-size:12px;color:#8b8b9e;margin-top:8px">📚 Uses FAISS vector search + semantic chunking</p>')
|
| 245 |
+
with gr.Column(scale=1, elem_classes="feature-card"):
|
| 246 |
+
p_out = gr.Textbox(label="Answer", lines=18, interactive=False, elem_classes="output-box")
|
| 247 |
+
|
| 248 |
+
def load_pdf_file(file):
|
| 249 |
+
if file is None:
|
| 250 |
+
return "", "⚠️ No file uploaded."
|
| 251 |
+
text = load_pdf(file.name)
|
| 252 |
+
word_count = len(text.split())
|
| 253 |
+
return text, f"✅ PDF loaded — {word_count:,} words extracted"
|
| 254 |
+
|
| 255 |
+
def answer_pdf_question(question, pdf_text):
|
| 256 |
+
if not pdf_text:
|
| 257 |
+
return "⚠️ Please upload a PDF first."
|
| 258 |
+
if not question.strip():
|
| 259 |
+
return "⚠️ Please enter a question."
|
| 260 |
+
graph = build_pdf_qa_graph()
|
| 261 |
+
result = graph.invoke({"pdf_text": pdf_text, "question": question, "context": "", "answer": ""})
|
| 262 |
+
return result["answer"]
|
| 263 |
+
|
| 264 |
+
p_file.change(fn=load_pdf_file, inputs=p_file, outputs=[pdf_state, p_status])
|
| 265 |
+
p_btn.click(fn=answer_pdf_question, inputs=[p_q, pdf_state], outputs=p_out)
|
| 266 |
+
|
| 267 |
+
gr.HTML("""
|
| 268 |
+
<div style="text-align:center;padding:2rem;color:#8b8b9e;font-size:13px;margin-top:1rem">
|
| 269 |
+
Built with LangChain · LangGraph · Groq · Gradio |
|
| 270 |
+
<a href="https://github.com/shashankheg" style="color:#6C5CE7;text-decoration:none">GitHub</a>
|
| 271 |
+
</div>
|
| 272 |
+
""")
|
| 273 |
+
|
| 274 |
+
return app
|
| 275 |
+
|
| 276 |
+
if __name__ == "__main__":
|
| 277 |
+
app = create_app()
|
| 278 |
+
app.launch(
|
| 279 |
+
server_name="0.0.0.0",
|
| 280 |
+
server_port=7860,
|
| 281 |
+
share=False
|
| 282 |
+
)
|
src/app1.py
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Main App using Gradio
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
#import all the libraries and functions
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from src.features.translation import translate_text
|
| 9 |
+
from src.features.summarization import summarize_text
|
| 10 |
+
from src.features.keyword_extraction import extract_keywords
|
| 11 |
+
from src.features.email_writer import write_email, write_cover_letter
|
| 12 |
+
from src.features.pdf_qa import ask_pdf, load_pdf
|
| 13 |
+
from src.graphs.chat_graph import ChatSession
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# === CHAT SESSION (persistent across turns) ===
|
| 18 |
+
chat_session = ChatSession()
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# === TRANSLATION TAB ===
|
| 22 |
+
def translation_tab():
|
| 23 |
+
with gr.Tab("🌐 Translation"):
|
| 24 |
+
gr.Markdown("## Language Translation\nTranslate text between 100+ languages.")
|
| 25 |
+
with gr.Row():
|
| 26 |
+
with gr.Column():
|
| 27 |
+
translate_input = gr.Textbox(
|
| 28 |
+
label="Text to Translate",
|
| 29 |
+
placeholder="Enter text here...",
|
| 30 |
+
lines=5
|
| 31 |
+
)
|
| 32 |
+
with gr.Row():
|
| 33 |
+
source_lang = gr.Dropdown(
|
| 34 |
+
label="Source Language",
|
| 35 |
+
choices=["English", "French", "Spanish", "German",
|
| 36 |
+
"Italian", "Portuguese", "Chinese", "Japanese",
|
| 37 |
+
"Arabic", "Hindi", "Korean", "Russian"],
|
| 38 |
+
value="English"
|
| 39 |
+
)
|
| 40 |
+
target_lang = gr.Dropdown(
|
| 41 |
+
label="Target Language",
|
| 42 |
+
choices=["English", "French", "Spanish", "German",
|
| 43 |
+
"Italian", "Portuguese", "Chinese", "Japanese",
|
| 44 |
+
"Arabic", "Hindi", "Korean", "Russian"],
|
| 45 |
+
value="French"
|
| 46 |
+
)
|
| 47 |
+
translate_btn = gr.Button("Translate 🌐", variant="primary")
|
| 48 |
+
with gr.Column():
|
| 49 |
+
translate_output = gr.Textbox(
|
| 50 |
+
label="Translation",
|
| 51 |
+
lines=5
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
translate_btn.click(
|
| 55 |
+
fn=translate_text,
|
| 56 |
+
inputs=[translate_input, source_lang, target_lang],
|
| 57 |
+
outputs=translate_output
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
#Summerization
|
| 63 |
+
def summarization_tab():
|
| 64 |
+
with gr.Tab("📝 Summarization"):
|
| 65 |
+
gr.Markdown("## Text Summarization\nSummarize long documents and articles.")
|
| 66 |
+
with gr.Row():
|
| 67 |
+
with gr.Column():
|
| 68 |
+
summary_input = gr.Textbox(
|
| 69 |
+
label="Text to Summarize",
|
| 70 |
+
placeholder="Paste your text here...",
|
| 71 |
+
lines=10
|
| 72 |
+
)
|
| 73 |
+
summary_mode = gr.Radio(
|
| 74 |
+
label="Summary Mode",
|
| 75 |
+
choices=["concise", "detailed"],
|
| 76 |
+
value="concise"
|
| 77 |
+
)
|
| 78 |
+
summary_btn = gr.Button("Summarize 📝", variant="primary")
|
| 79 |
+
with gr.Column():
|
| 80 |
+
summary_output = gr.Textbox(
|
| 81 |
+
label="Summary",
|
| 82 |
+
lines=10
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
summary_btn.click(
|
| 86 |
+
fn=summarize_text,
|
| 87 |
+
inputs=[summary_input, summary_mode],
|
| 88 |
+
outputs=summary_output
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# === KEYWORD EXTRACTION TAB ===
|
| 92 |
+
def keyword_tab():
|
| 93 |
+
with gr.Tab("🔍 Keywords"):
|
| 94 |
+
gr.Markdown("## Keyword Extraction\nExtract key topics from any text.")
|
| 95 |
+
with gr.Row():
|
| 96 |
+
with gr.Column():
|
| 97 |
+
keyword_input = gr.Textbox(
|
| 98 |
+
label="Text",
|
| 99 |
+
placeholder="Paste your text here...",
|
| 100 |
+
lines=8
|
| 101 |
+
)
|
| 102 |
+
num_keywords = gr.Slider(
|
| 103 |
+
label="Number of Keywords",
|
| 104 |
+
minimum=5,
|
| 105 |
+
maximum=20,
|
| 106 |
+
value=10,
|
| 107 |
+
step=1
|
| 108 |
+
)
|
| 109 |
+
keyword_btn = gr.Button("Extract Keywords 🔍", variant="primary")
|
| 110 |
+
with gr.Column():
|
| 111 |
+
keyword_output = gr.Textbox(
|
| 112 |
+
label="Keywords",
|
| 113 |
+
lines=8
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
keyword_btn.click(
|
| 117 |
+
fn=extract_keywords,
|
| 118 |
+
inputs=[keyword_input, num_keywords],
|
| 119 |
+
outputs=keyword_output
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# === EMAIL WRITER TAB ===
|
| 125 |
+
def email_tab():
|
| 126 |
+
with gr.Tab("✉️ Email Writer"):
|
| 127 |
+
gr.Markdown("## Email & Cover Letter Writer")
|
| 128 |
+
with gr.Tabs():
|
| 129 |
+
with gr.Tab("Email"):
|
| 130 |
+
with gr.Row():
|
| 131 |
+
with gr.Column():
|
| 132 |
+
email_type = gr.Dropdown(
|
| 133 |
+
label="Email Type",
|
| 134 |
+
choices=["follow-up", "introduction", "apology",
|
| 135 |
+
"request", "thank you", "complaint",
|
| 136 |
+
"proposal", "invitation"],
|
| 137 |
+
value="follow-up"
|
| 138 |
+
)
|
| 139 |
+
email_context = gr.Textbox(
|
| 140 |
+
label="Context / Purpose",
|
| 141 |
+
placeholder="Describe the purpose of the email...",
|
| 142 |
+
lines=4
|
| 143 |
+
)
|
| 144 |
+
email_tone = gr.Dropdown(
|
| 145 |
+
label="Tone",
|
| 146 |
+
choices=["professional", "friendly", "formal",
|
| 147 |
+
"casual", "urgent"],
|
| 148 |
+
value="professional"
|
| 149 |
+
)
|
| 150 |
+
with gr.Row():
|
| 151 |
+
email_recipient = gr.Textbox(
|
| 152 |
+
label="Recipient Name",
|
| 153 |
+
placeholder="Hiring Manager"
|
| 154 |
+
)
|
| 155 |
+
email_sender = gr.Textbox(
|
| 156 |
+
label="Your Name",
|
| 157 |
+
placeholder="Shashank"
|
| 158 |
+
)
|
| 159 |
+
email_btn = gr.Button("Write Email ✉️", variant="primary")
|
| 160 |
+
with gr.Column():
|
| 161 |
+
email_output = gr.Textbox(label="Generated Email", lines=15)
|
| 162 |
+
|
| 163 |
+
email_btn.click(
|
| 164 |
+
fn=write_email,
|
| 165 |
+
inputs=[email_type, email_context, email_tone,
|
| 166 |
+
email_recipient, email_sender],
|
| 167 |
+
outputs=email_output
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
with gr.Tab("Cover Letter"):
|
| 171 |
+
with gr.Row():
|
| 172 |
+
with gr.Column():
|
| 173 |
+
cl_name = gr.Textbox(label="Your Name", placeholder="Shashank Hegde")
|
| 174 |
+
cl_job = gr.Textbox(label="Job Title", placeholder="ML Engineer")
|
| 175 |
+
cl_company = gr.Textbox(label="Company", placeholder="Google")
|
| 176 |
+
cl_skills = gr.Textbox(label="Key Skills", placeholder="Python, ML, LangChain...", lines=3)
|
| 177 |
+
cl_exp = gr.Textbox(label="Experience Summary", placeholder="3 years in ML...", lines=3)
|
| 178 |
+
cl_btn = gr.Button("Write Cover Letter 📄", variant="primary")
|
| 179 |
+
with gr.Column():
|
| 180 |
+
cl_output = gr.Textbox(label="Cover Letter", lines=15)
|
| 181 |
+
|
| 182 |
+
cl_btn.click(
|
| 183 |
+
fn=write_cover_letter,
|
| 184 |
+
inputs=[cl_job, cl_company, cl_skills, cl_exp, cl_name],
|
| 185 |
+
outputs=cl_output
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
# === CHAT TAB ===
|
| 190 |
+
def chat_tab():
|
| 191 |
+
with gr.Tab("💬 Chat Assistant"):
|
| 192 |
+
gr.Markdown("## AI Chat Assistant\nAsk me anything!")
|
| 193 |
+
chatbot = gr.Chatbot(height=400)
|
| 194 |
+
with gr.Row():
|
| 195 |
+
chat_input = gr.Textbox(
|
| 196 |
+
label="Message",
|
| 197 |
+
placeholder="Type your message here...",
|
| 198 |
+
scale=4
|
| 199 |
+
)
|
| 200 |
+
chat_btn = gr.Button("Send 💬", variant="primary", scale=1)
|
| 201 |
+
clear_btn = gr.Button("Clear Conversation 🗑️")
|
| 202 |
+
|
| 203 |
+
def respond(message, history):
|
| 204 |
+
if not message.strip():
|
| 205 |
+
return "", history
|
| 206 |
+
response = chat_session.chat(message)
|
| 207 |
+
history.append((message, response))
|
| 208 |
+
return "", history
|
| 209 |
+
|
| 210 |
+
def clear():
|
| 211 |
+
chat_session.clear_history()
|
| 212 |
+
return []
|
| 213 |
+
|
| 214 |
+
chat_btn.click(
|
| 215 |
+
fn=respond,
|
| 216 |
+
inputs=[chat_input, chatbot],
|
| 217 |
+
outputs=[chat_input, chatbot]
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
clear_btn.click(
|
| 221 |
+
fn=clear,
|
| 222 |
+
inputs=[],
|
| 223 |
+
outputs=[chatbot]
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
chat_input.submit(
|
| 227 |
+
fn=respond,
|
| 228 |
+
inputs=[chat_input, chatbot],
|
| 229 |
+
outputs=[chat_input, chatbot]
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
clear_btn.click(fn=clear, outputs=chatbot)
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
# === PDF Q&A TAB ===
|
| 236 |
+
def pdf_tab():
|
| 237 |
+
pdf_text_state = gr.State("")
|
| 238 |
+
|
| 239 |
+
with gr.Tab("📄 PDF Q&A"):
|
| 240 |
+
gr.Markdown("## PDF Q&A\nUpload a PDF and ask questions about it.")
|
| 241 |
+
with gr.Row():
|
| 242 |
+
with gr.Column():
|
| 243 |
+
pdf_upload = gr.File(
|
| 244 |
+
label="Upload PDF",
|
| 245 |
+
file_types=[".pdf"]
|
| 246 |
+
)
|
| 247 |
+
pdf_status = gr.Textbox(label="Status", interactive=False)
|
| 248 |
+
pdf_question = gr.Textbox(
|
| 249 |
+
label="Ask a Question",
|
| 250 |
+
placeholder="What is this document about?",
|
| 251 |
+
lines=3
|
| 252 |
+
)
|
| 253 |
+
pdf_btn = gr.Button("Ask 🔍", variant="primary")
|
| 254 |
+
with gr.Column():
|
| 255 |
+
pdf_output = gr.Textbox(label="Answer", lines=15)
|
| 256 |
+
|
| 257 |
+
def load_pdf_file(file):
|
| 258 |
+
if file is None:
|
| 259 |
+
return "", "⚠️ No file uploaded."
|
| 260 |
+
text = load_pdf(file.name)
|
| 261 |
+
return text, f"✅ PDF loaded: {len(text.split())} words"
|
| 262 |
+
|
| 263 |
+
def answer_question(question, pdf_text):
|
| 264 |
+
if not pdf_text:
|
| 265 |
+
return "⚠️ Please upload a PDF first."
|
| 266 |
+
if not question.strip():
|
| 267 |
+
return "⚠️ Please enter a question."
|
| 268 |
+
import tempfile, os
|
| 269 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt',
|
| 270 |
+
delete=False) as f:
|
| 271 |
+
f.write(pdf_text)
|
| 272 |
+
tmp_path = f.name
|
| 273 |
+
|
| 274 |
+
from src.features.pdf_qa import build_pdf_qa_graph
|
| 275 |
+
graph = build_pdf_qa_graph()
|
| 276 |
+
result = graph.invoke({
|
| 277 |
+
"pdf_text": pdf_text,
|
| 278 |
+
"question": question,
|
| 279 |
+
"context": "",
|
| 280 |
+
"answer": ""
|
| 281 |
+
})
|
| 282 |
+
os.unlink(tmp_path)
|
| 283 |
+
return result["answer"]
|
| 284 |
+
|
| 285 |
+
pdf_upload.change(
|
| 286 |
+
fn=load_pdf_file,
|
| 287 |
+
inputs=pdf_upload,
|
| 288 |
+
outputs=[pdf_text_state, pdf_status]
|
| 289 |
+
)
|
| 290 |
+
pdf_btn.click(
|
| 291 |
+
fn=answer_question,
|
| 292 |
+
inputs=[pdf_question, pdf_text_state],
|
| 293 |
+
outputs=pdf_output
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
# === MAIN APP ===
|
| 297 |
+
def create_app():
|
| 298 |
+
with gr.Blocks(
|
| 299 |
+
title="🤖 GenAI Toolkit",
|
| 300 |
+
theme=gr.themes.Soft()
|
| 301 |
+
) as app:
|
| 302 |
+
gr.Markdown("""
|
| 303 |
+
# 🤖 GenAI Toolkit
|
| 304 |
+
### Powered by LangChain + LangGraph + Groq
|
| 305 |
+
""")
|
| 306 |
+
|
| 307 |
+
translation_tab()
|
| 308 |
+
summarization_tab()
|
| 309 |
+
keyword_tab()
|
| 310 |
+
email_tab()
|
| 311 |
+
chat_tab()
|
| 312 |
+
pdf_tab()
|
| 313 |
+
|
| 314 |
+
return app
|
| 315 |
+
|
| 316 |
+
if __name__ == "__main__":
|
| 317 |
+
app = create_app()
|
| 318 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|
src/features/Readme.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
The project structure is below.
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
GenAI-Toolkit/
|
| 6 |
+
├── src/
|
| 7 |
+
│ ├── features/
|
| 8 |
+
│ │ ├── translation.py # LangChain PromptTemplate
|
| 9 |
+
│ │ ├── summarization.py # LangChain MapReduceChain
|
| 10 |
+
│ │ ├── chat.py # LangGraph conversation graph
|
| 11 |
+
│ │ ├── pdf_qa.py # LangChain RAG + LangGraph agent
|
| 12 |
+
│ │ ├── email_writer.py # LangChain PromptTemplate
|
| 13 |
+
│ │ └── keyword_extraction.py # LangChain structured output
|
| 14 |
+
│ ├── graphs/
|
| 15 |
+
│ │ ├── chat_graph.py # LangGraph chat workflow
|
| 16 |
+
│ │ └── pdf_agent.py # LangGraph PDF Q&A agent
|
| 17 |
+
│ ├── utils/
|
| 18 |
+
│ │ ├── llm.py # OpenAI client setup
|
| 19 |
+
│ │ └── vector_store.py # FAISS setup
|
| 20 |
+
│ └── app.py # Gradio UI
|
| 21 |
+
├── data/
|
| 22 |
+
├── artifacts/
|
| 23 |
+
├── scripts/
|
| 24 |
+
│ └── download_datasets.py
|
| 25 |
+
├── .env
|
| 26 |
+
├── requirements.txt
|
| 27 |
+
├── Dockerfile
|
| 28 |
+
└── README.md
|
src/features/__pycache__/email_writer.cpython-311.pyc
ADDED
|
Binary file (3.6 kB). View file
|
|
|
src/features/__pycache__/keyword_extraction.cpython-311.pyc
ADDED
|
Binary file (1.82 kB). View file
|
|
|
src/features/__pycache__/pdf_qa.cpython-311.pyc
ADDED
|
Binary file (5.08 kB). View file
|
|
|
src/features/__pycache__/summarization.cpython-311.pyc
ADDED
|
Binary file (3.59 kB). View file
|
|
|
src/features/__pycache__/summarization.cpython-314.pyc
ADDED
|
Binary file (3.51 kB). View file
|
|
|
src/features/__pycache__/translation.cpython-311.pyc
ADDED
|
Binary file (1.63 kB). View file
|
|
|
src/features/email_writer.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.prompts import PromptTemplate
|
| 2 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 3 |
+
from src.utils.llm import get_llm
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
EMAIL_PROMPT = PromptTemplate(
|
| 8 |
+
input_variables=["email_type", "context", "tone", "recipient", "sender"],
|
| 9 |
+
template="""You are an expert professional email writer.
|
| 10 |
+
|
| 11 |
+
Write a {email_type} email with the following details:
|
| 12 |
+
- Recipient: {recipient}
|
| 13 |
+
- Sender: {sender}
|
| 14 |
+
- Tone: {tone}
|
| 15 |
+
- Context/Purpose: {context}
|
| 16 |
+
Requirements:
|
| 17 |
+
- Write a clear subject line
|
| 18 |
+
- Keep it professional and concise
|
| 19 |
+
- Match the requested tone exactly
|
| 20 |
+
- Include proper greeting and sign-off
|
| 21 |
+
|
| 22 |
+
Email:"""
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
COVER_LETTER_PROMPT = PromptTemplate(
|
| 26 |
+
input_variables=["job_title", "company", "skills", "experience", "name"],
|
| 27 |
+
template="""You are an expert cover letter writer.
|
| 28 |
+
|
| 29 |
+
Write a compelling cover letter for the following:
|
| 30 |
+
- Applicant Name: {name}
|
| 31 |
+
- Job Title: {job_title}
|
| 32 |
+
- Company: {company}
|
| 33 |
+
- Key Skills: {skills}
|
| 34 |
+
- Experience Summary: {experience}
|
| 35 |
+
|
| 36 |
+
Requirements:
|
| 37 |
+
- Start with a strong opening paragraph
|
| 38 |
+
- Highlight relevant skills and experience
|
| 39 |
+
- Show enthusiasm for the role and company
|
| 40 |
+
- End with a clear call to action
|
| 41 |
+
- Keep it to 3-4 paragraphs
|
| 42 |
+
|
| 43 |
+
Cover Letter:"""
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def write_email(
|
| 48 |
+
email_type: str,
|
| 49 |
+
context: str,
|
| 50 |
+
tone: str = "professional",
|
| 51 |
+
recipient: str = "Team",
|
| 52 |
+
sender: str = "Your Name"
|
| 53 |
+
) -> str:
|
| 54 |
+
"""
|
| 55 |
+
Generates a professional email.
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
email_type: Type of email (e.g. follow-up, apology, introduction)
|
| 59 |
+
context: Purpose or context of the email
|
| 60 |
+
tone: Tone of the email (professional, friendly, formal)
|
| 61 |
+
recipient: Name of the recipient
|
| 62 |
+
sender: Name of the sender
|
| 63 |
+
|
| 64 |
+
Returns:
|
| 65 |
+
Generated email string
|
| 66 |
+
"""
|
| 67 |
+
if not context.strip():
|
| 68 |
+
return "⚠️ Please provide context for the email."
|
| 69 |
+
llm = get_llm()
|
| 70 |
+
chain = EMAIL_PROMPT | llm | StrOutputParser()
|
| 71 |
+
return chain.invoke({
|
| 72 |
+
"email_type": email_type,
|
| 73 |
+
"context": context,
|
| 74 |
+
"tone": tone,
|
| 75 |
+
"recipient": recipient,
|
| 76 |
+
"sender": sender
|
| 77 |
+
})
|
| 78 |
+
|
| 79 |
+
def write_cover_letter(
|
| 80 |
+
job_title: str,
|
| 81 |
+
company: str,
|
| 82 |
+
skills: str,
|
| 83 |
+
experience: str,
|
| 84 |
+
name: str = "Your Name"
|
| 85 |
+
) -> str:
|
| 86 |
+
"""
|
| 87 |
+
Generates a cover letter.
|
| 88 |
+
|
| 89 |
+
Args:
|
| 90 |
+
job_title: Job title applying for
|
| 91 |
+
company: Company name
|
| 92 |
+
skills: Key skills relevant to the role
|
| 93 |
+
experience: Brief experience summary
|
| 94 |
+
name: Applicant name
|
| 95 |
+
|
| 96 |
+
Returns:
|
| 97 |
+
Generated cover letter string
|
| 98 |
+
"""
|
| 99 |
+
if not job_title.strip() or not company.strip():
|
| 100 |
+
return "⚠️ Please provide job title and company name."
|
| 101 |
+
|
| 102 |
+
llm = get_llm()
|
| 103 |
+
chain = COVER_LETTER_PROMPT | llm | StrOutputParser()
|
| 104 |
+
|
| 105 |
+
return chain.invoke({
|
| 106 |
+
"job_title": job_title,
|
| 107 |
+
"company": company,
|
| 108 |
+
"skills": skills,
|
| 109 |
+
"experience": experience,
|
| 110 |
+
"name": name
|
| 111 |
+
})
|
| 112 |
+
|
src/features/keyword_extraction.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.prompts import PromptTemplate
|
| 2 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 3 |
+
from src.utils.llm import get_fast_llm
|
| 4 |
+
|
| 5 |
+
KEYWORD_PROMPT = PromptTemplate(
|
| 6 |
+
input_variables=["text", "num_keywords"],
|
| 7 |
+
template="""You are an expert at extracting the most important keywords and key phrases from text.
|
| 8 |
+
Extract the top {num_keywords} keywords and key phrases from the following text.
|
| 9 |
+
Return them as a numbered list, with a brief explanation for each keyword.
|
| 10 |
+
|
| 11 |
+
Text:
|
| 12 |
+
{text}
|
| 13 |
+
|
| 14 |
+
Keywords:"""
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def extract_keywords(text: str, num_keywords: int = 10) -> str:
|
| 19 |
+
"""
|
| 20 |
+
Extracts keywords from the given text.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
text: Text to extract keywords from
|
| 24 |
+
num_keywords: Number of keywords to extract
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
Numbered list of keywords with explanations
|
| 28 |
+
"""
|
| 29 |
+
if not text.strip():
|
| 30 |
+
return "⚠️ Please enter text to extract keywords from."
|
| 31 |
+
|
| 32 |
+
if len(text.split()) < 10:
|
| 33 |
+
return "⚠️ Please enter more text for meaningful keyword extraction."
|
| 34 |
+
|
| 35 |
+
llm = get_fast_llm()
|
| 36 |
+
chain = KEYWORD_PROMPT | llm | StrOutputParser()
|
| 37 |
+
|
| 38 |
+
result = chain.invoke({
|
| 39 |
+
"text": text,
|
| 40 |
+
"num_keywords": num_keywords
|
| 41 |
+
})
|
| 42 |
+
|
| 43 |
+
return result
|
src/features/pdf_qa.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fitz # PyMuPDF
|
| 2 |
+
from typing import Annotated, TypedDict, List
|
| 3 |
+
|
| 4 |
+
from langgraph.graph import StateGraph, END
|
| 5 |
+
from langchain_core.prompts import PromptTemplate
|
| 6 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 8 |
+
from src.utils.llm import get_llm
|
| 9 |
+
from src.utils.vector_store import create_vector_store
|
| 10 |
+
|
| 11 |
+
# === PROMPT ===
|
| 12 |
+
QA_PROMPT = PromptTemplate(
|
| 13 |
+
input_variables=["context", "question"],
|
| 14 |
+
template="""You are an expert at answering questions based on provided document context.
|
| 15 |
+
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
| 16 |
+
|
| 17 |
+
{context}
|
| 18 |
+
|
| 19 |
+
Question: {question}
|
| 20 |
+
Answer:"""
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# === STATE ===
|
| 25 |
+
class PDFQAState(TypedDict):
|
| 26 |
+
pdf_text: str
|
| 27 |
+
question: str
|
| 28 |
+
context: str
|
| 29 |
+
answer: str
|
| 30 |
+
|
| 31 |
+
# === PDF LOADING ===
|
| 32 |
+
def load_pdf(pdf_path: str) -> str:
|
| 33 |
+
"""Extracts text from a PDF file."""
|
| 34 |
+
doc = fitz.open(pdf_path)
|
| 35 |
+
text = ""
|
| 36 |
+
for page in doc:
|
| 37 |
+
text += page.get_text()
|
| 38 |
+
doc.close()
|
| 39 |
+
return text
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# === NODES ===
|
| 43 |
+
def retrieve_node(state: PDFQAState) -> PDFQAState:
|
| 44 |
+
"""Retrieves relevant chunks from the PDF."""
|
| 45 |
+
splitter = RecursiveCharacterTextSplitter(
|
| 46 |
+
chunk_size=1000,
|
| 47 |
+
chunk_overlap=100
|
| 48 |
+
)
|
| 49 |
+
chunks = splitter.split_text(state["pdf_text"])
|
| 50 |
+
vector_store = create_vector_store(chunks)
|
| 51 |
+
|
| 52 |
+
# Get top 3 relevant chunks
|
| 53 |
+
docs = vector_store.similarity_search(state["question"], k=3)
|
| 54 |
+
context = "\n\n".join([doc.page_content for doc in docs])
|
| 55 |
+
return {**state, "context": context}
|
| 56 |
+
|
| 57 |
+
def answer_node(state: PDFQAState) -> PDFQAState:
|
| 58 |
+
"""Generates answer from context."""
|
| 59 |
+
llm = get_llm()
|
| 60 |
+
prompt = QA_PROMPT
|
| 61 |
+
output_parser = StrOutputParser()
|
| 62 |
+
|
| 63 |
+
chain = prompt | llm | output_parser
|
| 64 |
+
answer = chain.invoke(
|
| 65 |
+
{"context": state["context"],
|
| 66 |
+
"question": state["question"]}
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
return {**state, "answer": answer}
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
#building the graph
|
| 73 |
+
def build_pdf_qa_graph():
|
| 74 |
+
graph = StateGraph(PDFQAState)
|
| 75 |
+
graph.add_node("retrieve", retrieve_node)
|
| 76 |
+
graph.add_node("answer", answer_node)
|
| 77 |
+
graph.set_entry_point("retrieve")
|
| 78 |
+
graph.add_edge("retrieve", "answer")
|
| 79 |
+
graph.add_edge("answer", END)
|
| 80 |
+
return graph.compile()
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# === MAIN FUNCTION ===
|
| 85 |
+
def ask_pdf(pdf_path: str, question: str) -> str:
|
| 86 |
+
"""
|
| 87 |
+
Ask a question about a PDF document.
|
| 88 |
+
|
| 89 |
+
Args:
|
| 90 |
+
pdf_path: Path to the PDF file
|
| 91 |
+
question: Question to ask
|
| 92 |
+
|
| 93 |
+
Returns:
|
| 94 |
+
Answer string
|
| 95 |
+
"""
|
| 96 |
+
if not question.strip():
|
| 97 |
+
return "⚠️ Please enter a question."
|
| 98 |
+
|
| 99 |
+
print(f"📄 Loading PDF: {pdf_path}")
|
| 100 |
+
pdf_text = load_pdf(pdf_path)
|
| 101 |
+
|
| 102 |
+
if not pdf_text.strip():
|
| 103 |
+
return "⚠️ Could not extract text from PDF."
|
| 104 |
+
|
| 105 |
+
print(f"🔍 Finding relevant context...")
|
| 106 |
+
graph = build_pdf_qa_graph()
|
| 107 |
+
result = graph.invoke({
|
| 108 |
+
"pdf_text": pdf_text,
|
| 109 |
+
"question": question,
|
| 110 |
+
"context": "",
|
| 111 |
+
"answer": ""
|
| 112 |
+
})
|
| 113 |
+
|
| 114 |
+
return result["answer"]
|
src/features/summarization.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.prompts import PromptTemplate
|
| 2 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 3 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 4 |
+
from src.utils.llm import get_llm, get_fast_llm
|
| 5 |
+
|
| 6 |
+
# Prompt for short text summarization
|
| 7 |
+
|
| 8 |
+
SHORT_SUMMARY_PROMPT = PromptTemplate(
|
| 9 |
+
input_variables=["text"],
|
| 10 |
+
template="""You are an expert at summarizing content clearly and concisely.
|
| 11 |
+
|
| 12 |
+
Summarize the following text in a clear, concise way.
|
| 13 |
+
Capture the key points and main ideas.
|
| 14 |
+
|
| 15 |
+
Text : {text}
|
| 16 |
+
|
| 17 |
+
Summary :"""
|
| 18 |
+
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Prompt for summarizing each chunk (map step)
|
| 23 |
+
|
| 24 |
+
MAP_PROMPT = PromptTemplate(
|
| 25 |
+
input_variables=["text"],
|
| 26 |
+
template="""Summarize the following section, capturing all key points:
|
| 27 |
+
|
| 28 |
+
{text}
|
| 29 |
+
|
| 30 |
+
Section Summary:"""
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Prompt for combining chunk summaries (reduce step)
|
| 35 |
+
REDUCE_PROMPT = PromptTemplate(
|
| 36 |
+
input_variables=["text"],
|
| 37 |
+
template="""You are given multiple summaries of different sections of a document.
|
| 38 |
+
Combine them into one final, coherent summary that captures all key points.
|
| 39 |
+
|
| 40 |
+
Summaries:
|
| 41 |
+
{text}
|
| 42 |
+
|
| 43 |
+
Final Summary:"""
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
def summarize_text(text: str, mode: str = "concise") -> str:
|
| 47 |
+
"""
|
| 48 |
+
Summarizes text. Handles both short and long documents.
|
| 49 |
+
|
| 50 |
+
Args:
|
| 51 |
+
text: Text to summarize
|
| 52 |
+
mode: 'concise' for short summary, 'detailed' for longer summary
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
Summary string
|
| 56 |
+
"""
|
| 57 |
+
if not text.strip():
|
| 58 |
+
return "⚠️ Please enter text to summarize."
|
| 59 |
+
|
| 60 |
+
if len(text.split())<1000 :
|
| 61 |
+
llm =get_fast_llm()
|
| 62 |
+
chain = SHORT_SUMMARY_PROMPT | llm | StrOutputParser()
|
| 63 |
+
return chain.invoke({"text": text})
|
| 64 |
+
#Use MAP REDUCE concept for longer string
|
| 65 |
+
|
| 66 |
+
return _map_reduce_summarize(text, mode)
|
| 67 |
+
|
| 68 |
+
def _map_reduce_summarize(text: str, mode: str) -> str:
|
| 69 |
+
"""Handles long document summarization using map-reduce."""
|
| 70 |
+
# Split text into chunks
|
| 71 |
+
splitter = RecursiveCharacterTextSplitter(
|
| 72 |
+
chunk_size=3000,
|
| 73 |
+
chunk_overlap=200
|
| 74 |
+
)
|
| 75 |
+
chunks = splitter.split_text(text)
|
| 76 |
+
print(f"📄 Split into {len(chunks)} chunks for summarization...")
|
| 77 |
+
|
| 78 |
+
llm = get_fast_llm()
|
| 79 |
+
parser = StrOutputParser()
|
| 80 |
+
|
| 81 |
+
# Map — summarize each chunk
|
| 82 |
+
map_chain = MAP_PROMPT | llm | parser
|
| 83 |
+
chunk_summaries = []
|
| 84 |
+
for i, chunk in enumerate(chunks):
|
| 85 |
+
summary = map_chain.invoke({"text": chunk})
|
| 86 |
+
chunk_summaries.append(summary)
|
| 87 |
+
print(f"📝 Summarized chunk {i + 1}/{len(chunks)}")
|
| 88 |
+
|
| 89 |
+
# Reduce — combine all chunk summaries
|
| 90 |
+
reduce_chain = REDUCE_PROMPT | llm | parser
|
| 91 |
+
final_summary = reduce_chain.invoke({"text": "\n".join(chunk_summaries)})
|
| 92 |
+
|
| 93 |
+
return final_summary
|
src/features/translation.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_openai import ChatOpenAI
|
| 2 |
+
from langchain_core.prompts import PromptTemplate
|
| 3 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 4 |
+
from src.utils.llm import get_fast_llm
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# Prompt template for translation
|
| 9 |
+
TRANSLATION_PROMPT = PromptTemplate(
|
| 10 |
+
input_variables=["text", "source_lang", "target_lang"],
|
| 11 |
+
template=(
|
| 12 |
+
"""You are an expert translator.
|
| 13 |
+
Translate the following text from {source_lang} to {target_lang}.
|
| 14 |
+
Preserve the original tone, style, and meaning as accurately as possible.
|
| 15 |
+
|
| 16 |
+
Text to translate:
|
| 17 |
+
{text}
|
| 18 |
+
Translation:"""
|
| 19 |
+
))
|
| 20 |
+
|
| 21 |
+
#Functions to translate text
|
| 22 |
+
def translate_text(text: str, source_lang: str, target_lang: str) -> str:
|
| 23 |
+
"""
|
| 24 |
+
Translates text from source language to target language.
|
| 25 |
+
Args:
|
| 26 |
+
text (str): The text to translate.
|
| 27 |
+
source_lang (str): The language of the input text.
|
| 28 |
+
target_lang (str): The language to translate the text into.
|
| 29 |
+
Returns:
|
| 30 |
+
str: The translated text.
|
| 31 |
+
"""
|
| 32 |
+
llm = get_fast_llm()
|
| 33 |
+
chain = TRANSLATION_PROMPT | llm | StrOutputParser()
|
| 34 |
+
result = chain.invoke({
|
| 35 |
+
"text": text,
|
| 36 |
+
"source_lang": source_lang,
|
| 37 |
+
"target_lang": target_lang
|
| 38 |
+
})
|
| 39 |
+
|
| 40 |
+
return result
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
src/graphs/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
from .chat_graph import ChatSession
|
src/graphs/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (219 Bytes). View file
|
|
|
src/graphs/__pycache__/chat_graph.cpython-311.pyc
ADDED
|
Binary file (3.8 kB). View file
|
|
|
src/graphs/chat_graph.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Annotated, TypedDict, List #Better code writing for readabality
|
| 2 |
+
from langgraph.graph import StateGraph, END
|
| 3 |
+
from langgraph.graph.message import add_messages
|
| 4 |
+
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
|
| 5 |
+
from src.utils.llm import get_llm
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# === STATE DEFINITION ===
|
| 10 |
+
|
| 11 |
+
class ChatState(TypedDict):
|
| 12 |
+
messages : Annotated[list, add_messages]
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# === SYSTEM PROMPT ===
|
| 18 |
+
|
| 19 |
+
SYSTEM_PROMPT = SystemMessage(content="""You are a helpful assistant.
|
| 20 |
+
|
| 21 |
+
You provide clear, accurate, and concise responses.
|
| 22 |
+
If you don't know something, you say so honestly.
|
| 23 |
+
You maintain context throughout the conversation.""")
|
| 24 |
+
|
| 25 |
+
# === NODES ===
|
| 26 |
+
def chat_node(state: ChatState) -> ChatState:
|
| 27 |
+
# Get the last message from the state
|
| 28 |
+
last_message = state["messages"][-1]
|
| 29 |
+
|
| 30 |
+
# Get the LLM
|
| 31 |
+
llm = get_llm()
|
| 32 |
+
|
| 33 |
+
# Generate a response
|
| 34 |
+
response = llm.invoke([SYSTEM_PROMPT] + state["messages"])
|
| 35 |
+
|
| 36 |
+
# Add the response to the state
|
| 37 |
+
state["messages"].append(response)
|
| 38 |
+
|
| 39 |
+
return state
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# === BUILD GRAPH ===
|
| 43 |
+
def build_chat_graph():
|
| 44 |
+
graph = StateGraph(ChatState)
|
| 45 |
+
graph.add_node("chat", chat_node)
|
| 46 |
+
graph.set_entry_point("chat")
|
| 47 |
+
graph.add_edge("chat", END)
|
| 48 |
+
return graph.compile()
|
| 49 |
+
|
| 50 |
+
# === CHAT SESSION ===
|
| 51 |
+
class ChatSession:
|
| 52 |
+
"""Manages a multi-turn chat conversation."""
|
| 53 |
+
|
| 54 |
+
def __init__(self):
|
| 55 |
+
self.graph = build_chat_graph()
|
| 56 |
+
self.history: List = []
|
| 57 |
+
|
| 58 |
+
def chat(self, user_message: str) -> str:
|
| 59 |
+
"""
|
| 60 |
+
Send a message and get a response.
|
| 61 |
+
|
| 62 |
+
Args:
|
| 63 |
+
user_message: User input string
|
| 64 |
+
|
| 65 |
+
Returns:
|
| 66 |
+
AI response string
|
| 67 |
+
"""
|
| 68 |
+
if not user_message.strip():
|
| 69 |
+
return "⚠️ Please enter a message."
|
| 70 |
+
# Add user message to history
|
| 71 |
+
self.history.append(HumanMessage(content=user_message))
|
| 72 |
+
|
| 73 |
+
# Run graph
|
| 74 |
+
result = self.graph.invoke({"messages": self.history})
|
| 75 |
+
|
| 76 |
+
# Extract and store response
|
| 77 |
+
ai_message = result["messages"][-1]
|
| 78 |
+
self.history.append(ai_message)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
return ai_message.content
|
| 82 |
+
|
| 83 |
+
def clear_history(self):
|
| 84 |
+
"""Clears conversation history."""
|
| 85 |
+
self.history = []
|
| 86 |
+
return "✅ Conversation cleared."
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
src/requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
gradio
|
| 3 |
+
langchain
|
| 4 |
+
langchain-openai
|
| 5 |
+
langgraph
|
| 6 |
+
langchain-community
|
| 7 |
+
faiss-cpu
|
| 8 |
+
pymupdf
|
| 9 |
+
python-dotenv
|
| 10 |
+
tiktoken
|
src/utils/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
from src.utils.llm import get_llm, get_fast_llm, get_embeddings
|
src/utils/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (277 Bytes). View file
|
|
|
src/utils/__pycache__/llm.cpython-311.pyc
ADDED
|
Binary file (1.8 kB). View file
|
|
|
src/utils/__pycache__/vector_store.cpython-311.pyc
ADDED
|
Binary file (1.62 kB). View file
|
|
|
src/utils/llm.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
from langchain_openai import OpenAIEmbeddings
|
| 5 |
+
|
| 6 |
+
load_dotenv(dotenv_path=os.path.join(os.path.dirname(__file__), "../../.env"))
|
| 7 |
+
|
| 8 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
+
if not GROQ_API_KEY:
|
| 10 |
+
raise ValueError("GROQ_API_KEY is not set in the environment variables.")
|
| 11 |
+
|
| 12 |
+
def get_llm(model: str = "llama-3.3-70b-versatile", temperature: float = 0.7):
|
| 13 |
+
"""Returns a ChatGroq instance for complex tasks."""
|
| 14 |
+
return ChatGroq(
|
| 15 |
+
model=model,
|
| 16 |
+
temperature=temperature,
|
| 17 |
+
api_key=GROQ_API_KEY
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
def get_fast_llm():
|
| 21 |
+
"""Faster model for simple tasks."""
|
| 22 |
+
return ChatGroq(
|
| 23 |
+
model="llama-3.1-8b-instant",
|
| 24 |
+
temperature=0.3,
|
| 25 |
+
api_key=GROQ_API_KEY
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
def get_embeddings():
|
| 29 |
+
"""Returns OpenAI embeddings for vector store (still needs OpenAI key)."""
|
| 30 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 31 |
+
return OpenAIEmbeddings(
|
| 32 |
+
model="text-embedding-3-small",
|
| 33 |
+
api_key=OPENAI_API_KEY
|
| 34 |
+
)
|
src/utils/vector_store.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain_community.vectorstores import FAISS
|
| 3 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 4 |
+
|
| 5 |
+
# Use free HuggingFace embeddings instead of OpenAI
|
| 6 |
+
|
| 7 |
+
def get_embeddings():
|
| 8 |
+
return HuggingFaceEmbeddings(
|
| 9 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def create_vector_store(chunks):
|
| 15 |
+
"""Creates FAISS vector store from text chunks."""
|
| 16 |
+
embeddings = get_embeddings()
|
| 17 |
+
vector_store = FAISS.from_texts(chunks, embedding=embeddings)
|
| 18 |
+
|
| 19 |
+
return vector_store
|
| 20 |
+
|
| 21 |
+
def save_vector_store(vector_store, path: str):
|
| 22 |
+
"""Saves vector store to disk."""
|
| 23 |
+
os.makedirs(path, exist_ok=True)
|
| 24 |
+
vector_store.save_local(path)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def load_vector_store(path: str):
|
| 28 |
+
"""Loads vector store from disk."""
|
| 29 |
+
embeddings = get_embeddings()
|
| 30 |
+
return FAISS.load_local(path, embeddings, allow_dangerous_deserialization=True)
|
| 31 |
+
|