Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import os
|
|
| 4 |
from fpdf import FPDF
|
| 5 |
|
| 6 |
# Initialize Groq client
|
| 7 |
-
# Ensure your environment variable is set!
|
| 8 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 9 |
|
| 10 |
# ---------- ROADMAP GENERATOR ----------
|
|
@@ -36,9 +35,7 @@ def download_pdf(text):
|
|
| 36 |
pdf.add_page()
|
| 37 |
pdf.set_font("Arial", size=12)
|
| 38 |
|
| 39 |
-
# Clean text to avoid encoding issues with FPDF
|
| 40 |
clean_text = text.encode('latin-1', 'ignore').decode('latin-1')
|
| 41 |
-
|
| 42 |
for line in clean_text.split("\n"):
|
| 43 |
pdf.multi_cell(0, 10, line)
|
| 44 |
|
|
@@ -48,16 +45,11 @@ def download_pdf(text):
|
|
| 48 |
|
| 49 |
# ---------- AI CHATBOT ----------
|
| 50 |
def chat(message, history):
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
# Build
|
| 55 |
-
messages =
|
| 56 |
-
for entry in history:
|
| 57 |
-
messages.append({"role": "user", "content": entry[0]})
|
| 58 |
-
messages.append({"role": "assistant", "content": entry[1]})
|
| 59 |
-
|
| 60 |
-
# Add current user message
|
| 61 |
messages.append({"role": "user", "content": message})
|
| 62 |
|
| 63 |
try:
|
|
@@ -69,9 +61,11 @@ def chat(message, history):
|
|
| 69 |
except Exception as e:
|
| 70 |
reply = f"Error: {str(e)}"
|
| 71 |
|
| 72 |
-
# Update history
|
| 73 |
-
history.append(
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# ---------- GRADIO UI ----------
|
| 77 |
with gr.Blocks() as app:
|
|
@@ -94,9 +88,8 @@ with gr.Blocks() as app:
|
|
| 94 |
with gr.Tab("AI Chatbot"):
|
| 95 |
chatbot = gr.Chatbot()
|
| 96 |
msg = gr.Textbox(label="Your Message", placeholder="Type and press Enter...")
|
| 97 |
-
|
| 98 |
-
# We use the chatbot component itself to store history
|
| 99 |
msg.submit(chat, [msg, chatbot], [msg, chatbot])
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
-
app.launch()
|
|
|
|
| 4 |
from fpdf import FPDF
|
| 5 |
|
| 6 |
# Initialize Groq client
|
|
|
|
| 7 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
|
| 9 |
# ---------- ROADMAP GENERATOR ----------
|
|
|
|
| 35 |
pdf.add_page()
|
| 36 |
pdf.set_font("Arial", size=12)
|
| 37 |
|
|
|
|
| 38 |
clean_text = text.encode('latin-1', 'ignore').decode('latin-1')
|
|
|
|
| 39 |
for line in clean_text.split("\n"):
|
| 40 |
pdf.multi_cell(0, 10, line)
|
| 41 |
|
|
|
|
| 45 |
|
| 46 |
# ---------- AI CHATBOT ----------
|
| 47 |
def chat(message, history):
|
| 48 |
+
if history is None:
|
| 49 |
+
history = []
|
| 50 |
+
|
| 51 |
+
# Build message list for Groq API
|
| 52 |
+
messages = history.copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
messages.append({"role": "user", "content": message})
|
| 54 |
|
| 55 |
try:
|
|
|
|
| 61 |
except Exception as e:
|
| 62 |
reply = f"Error: {str(e)}"
|
| 63 |
|
| 64 |
+
# Update history
|
| 65 |
+
history.append({"role": "user", "content": message})
|
| 66 |
+
history.append({"role": "assistant", "content": reply})
|
| 67 |
+
|
| 68 |
+
return "", history # Clear input box, return updated history
|
| 69 |
|
| 70 |
# ---------- GRADIO UI ----------
|
| 71 |
with gr.Blocks() as app:
|
|
|
|
| 88 |
with gr.Tab("AI Chatbot"):
|
| 89 |
chatbot = gr.Chatbot()
|
| 90 |
msg = gr.Textbox(label="Your Message", placeholder="Type and press Enter...")
|
| 91 |
+
|
|
|
|
| 92 |
msg.submit(chat, [msg, chatbot], [msg, chatbot])
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
| 95 |
+
app.launch()
|