Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -71,46 +71,67 @@
|
|
| 71 |
import os
|
| 72 |
from groq import Groq
|
| 73 |
import gradio as gr
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
#
|
| 76 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 77 |
|
| 78 |
-
# System prompt
|
| 79 |
SYSTEM_PROMPT = {
|
| 80 |
"role": "system",
|
| 81 |
-
"content": "You are a
|
| 82 |
}
|
| 83 |
|
| 84 |
-
#
|
| 85 |
def read_file(file):
|
| 86 |
if file is None:
|
| 87 |
return ""
|
|
|
|
| 88 |
try:
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
except:
|
| 91 |
return ""
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
def bot(history, file):
|
| 95 |
|
| 96 |
messages = [SYSTEM_PROMPT]
|
| 97 |
|
| 98 |
-
#
|
| 99 |
file_text = read_file(file)
|
| 100 |
if file_text:
|
| 101 |
messages.append({
|
| 102 |
"role": "user",
|
| 103 |
-
"content": f"
|
| 104 |
})
|
| 105 |
|
| 106 |
-
#
|
| 107 |
for msg in history[:-1]:
|
| 108 |
messages.append({
|
| 109 |
"role": msg["role"],
|
| 110 |
"content": str(msg["content"])
|
| 111 |
})
|
| 112 |
|
| 113 |
-
# Latest user
|
| 114 |
user_msg = history[-1]["content"]
|
| 115 |
messages.append({"role": "user", "content": user_msg})
|
| 116 |
|
|
@@ -127,12 +148,10 @@ def bot(history, file):
|
|
| 127 |
if chunk.choices[0].delta.content:
|
| 128 |
response += chunk.choices[0].delta.content
|
| 129 |
|
| 130 |
-
# Update assistant message
|
| 131 |
history[-1] = {
|
| 132 |
"role": "assistant",
|
| 133 |
"content": response
|
| 134 |
}
|
| 135 |
-
|
| 136 |
yield history
|
| 137 |
|
| 138 |
except Exception as e:
|
|
@@ -142,33 +161,55 @@ def bot(history, file):
|
|
| 142 |
}
|
| 143 |
yield history
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
# UI
|
| 147 |
with gr.Blocks() as demo:
|
| 148 |
|
| 149 |
-
gr.Markdown("# π Groq AI Chatbot")
|
| 150 |
-
gr.Markdown("π¬
|
| 151 |
|
| 152 |
chatbot = gr.Chatbot(height=400)
|
| 153 |
|
| 154 |
with gr.Row():
|
| 155 |
-
msg = gr.Textbox(placeholder="Type
|
| 156 |
-
file = gr.File(scale=1)
|
| 157 |
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
-
|
|
|
|
|
|
|
| 161 |
def user_input(message, history):
|
| 162 |
history = history or []
|
| 163 |
-
history.append({
|
| 164 |
-
"role": "user",
|
| 165 |
-
"content": message
|
| 166 |
-
})
|
| 167 |
return "", history
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
send.click(user_input, [msg, chatbot], [msg, chatbot]) \
|
| 170 |
.then(bot, [chatbot, file], chatbot)
|
| 171 |
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
| 173 |
if __name__ == "__main__":
|
| 174 |
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 71 |
import os
|
| 72 |
from groq import Groq
|
| 73 |
import gradio as gr
|
| 74 |
+
from PyPDF2 import PdfReader
|
| 75 |
+
import tempfile
|
| 76 |
|
| 77 |
+
# Groq client
|
| 78 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 79 |
|
|
|
|
| 80 |
SYSTEM_PROMPT = {
|
| 81 |
"role": "system",
|
| 82 |
+
"content": "You are a smart, friendly AI assistant."
|
| 83 |
}
|
| 84 |
|
| 85 |
+
# -------- FILE HANDLING --------
|
| 86 |
def read_file(file):
|
| 87 |
if file is None:
|
| 88 |
return ""
|
| 89 |
+
|
| 90 |
try:
|
| 91 |
+
# TEXT FILE
|
| 92 |
+
if file.name.endswith(".txt"):
|
| 93 |
+
return file.read().decode("utf-8", errors="ignore")[:3000]
|
| 94 |
+
|
| 95 |
+
# PDF FILE
|
| 96 |
+
elif file.name.endswith(".pdf"):
|
| 97 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp:
|
| 98 |
+
tmp.write(file.read())
|
| 99 |
+
tmp_path = tmp.name
|
| 100 |
+
|
| 101 |
+
reader = PdfReader(tmp_path)
|
| 102 |
+
text = ""
|
| 103 |
+
|
| 104 |
+
for page in reader.pages:
|
| 105 |
+
text += page.extract_text() or ""
|
| 106 |
+
|
| 107 |
+
return text[:3000]
|
| 108 |
+
|
| 109 |
except:
|
| 110 |
return ""
|
| 111 |
|
| 112 |
+
return ""
|
| 113 |
+
|
| 114 |
+
# -------- CHAT FUNCTION (STREAMING) --------
|
| 115 |
def bot(history, file):
|
| 116 |
|
| 117 |
messages = [SYSTEM_PROMPT]
|
| 118 |
|
| 119 |
+
# File content
|
| 120 |
file_text = read_file(file)
|
| 121 |
if file_text:
|
| 122 |
messages.append({
|
| 123 |
"role": "user",
|
| 124 |
+
"content": f"Use this document:\n{file_text}"
|
| 125 |
})
|
| 126 |
|
| 127 |
+
# History
|
| 128 |
for msg in history[:-1]:
|
| 129 |
messages.append({
|
| 130 |
"role": msg["role"],
|
| 131 |
"content": str(msg["content"])
|
| 132 |
})
|
| 133 |
|
| 134 |
+
# Latest user msg
|
| 135 |
user_msg = history[-1]["content"]
|
| 136 |
messages.append({"role": "user", "content": user_msg})
|
| 137 |
|
|
|
|
| 148 |
if chunk.choices[0].delta.content:
|
| 149 |
response += chunk.choices[0].delta.content
|
| 150 |
|
|
|
|
| 151 |
history[-1] = {
|
| 152 |
"role": "assistant",
|
| 153 |
"content": response
|
| 154 |
}
|
|
|
|
| 155 |
yield history
|
| 156 |
|
| 157 |
except Exception as e:
|
|
|
|
| 161 |
}
|
| 162 |
yield history
|
| 163 |
|
| 164 |
+
# -------- VOICE INPUT --------
|
| 165 |
+
def speech_to_text(audio):
|
| 166 |
+
if audio is None:
|
| 167 |
+
return ""
|
| 168 |
+
return "Voice input received (speech-to-text can be added here)"
|
| 169 |
|
| 170 |
+
# -------- UI --------
|
| 171 |
with gr.Blocks() as demo:
|
| 172 |
|
| 173 |
+
gr.Markdown("# π Advanced Groq AI Chatbot")
|
| 174 |
+
gr.Markdown("π¬ Chat | π PDF | π€ Voice | π Theme Toggle")
|
| 175 |
|
| 176 |
chatbot = gr.Chatbot(height=400)
|
| 177 |
|
| 178 |
with gr.Row():
|
| 179 |
+
msg = gr.Textbox(placeholder="Type message...", scale=4)
|
| 180 |
+
file = gr.File(file_types=[".pdf", ".txt"], scale=1)
|
| 181 |
|
| 182 |
+
with gr.Row():
|
| 183 |
+
audio = gr.Audio(type="filepath", label="π€ Voice Input")
|
| 184 |
+
send = gr.Button("Send π")
|
| 185 |
+
|
| 186 |
+
# Theme toggle (Dark/Light)
|
| 187 |
+
theme_toggle = gr.Checkbox(label="π Dark Mode")
|
| 188 |
+
|
| 189 |
+
def toggle_theme(dark):
|
| 190 |
+
return gr.themes.Soft() if not dark else gr.themes.Monochrome()
|
| 191 |
|
| 192 |
+
theme_toggle.change(toggle_theme, inputs=theme_toggle, outputs=[])
|
| 193 |
+
|
| 194 |
+
# User input
|
| 195 |
def user_input(message, history):
|
| 196 |
history = history or []
|
| 197 |
+
history.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
| 198 |
return "", history
|
| 199 |
|
| 200 |
+
# Voice input -> text
|
| 201 |
+
def voice_input(audio, history):
|
| 202 |
+
text = speech_to_text(audio)
|
| 203 |
+
if text:
|
| 204 |
+
history.append({"role": "user", "content": text})
|
| 205 |
+
return history
|
| 206 |
+
|
| 207 |
send.click(user_input, [msg, chatbot], [msg, chatbot]) \
|
| 208 |
.then(bot, [chatbot, file], chatbot)
|
| 209 |
|
| 210 |
+
audio.change(voice_input, [audio, chatbot], chatbot) \
|
| 211 |
+
.then(bot, [chatbot, file], chatbot)
|
| 212 |
+
|
| 213 |
+
# Launch
|
| 214 |
if __name__ == "__main__":
|
| 215 |
demo.launch(theme=gr.themes.Soft())
|