Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,15 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import zipfile
|
| 4 |
-
import requests
|
| 5 |
-
import json
|
| 6 |
-
from pathlib import Path
|
| 7 |
import logging
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
from sentence_transformers import SentenceTransformer
|
| 10 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 11 |
import faiss
|
| 12 |
from simple_salesforce import Salesforce
|
| 13 |
-
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Setup logging
|
| 16 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -134,8 +133,6 @@ def create_salesforce_record(query, answer, confidence_percentage, source_link):
|
|
| 134 |
except Exception as e:
|
| 135 |
# Log any error during record creation
|
| 136 |
logger.error(f"Error creating Salesforce record: {str(e)}")
|
| 137 |
-
if 'response' in locals():
|
| 138 |
-
logger.error(f"Salesforce API Response: {str(response)}")
|
| 139 |
return None
|
| 140 |
|
| 141 |
# --- Search & Answer ---
|
|
@@ -184,14 +181,19 @@ def answer_query(query):
|
|
| 184 |
logger.error(f"Error in answer_query: {str(e)}")
|
| 185 |
return f"Error: {str(e)}", "", "", ""
|
| 186 |
|
| 187 |
-
# --- Gradio UI ---
|
| 188 |
-
def process_question(q):
|
| 189 |
if not q.strip():
|
| 190 |
-
return "Please enter a question.", "", ""
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
with gr.Blocks(title="Company Documents Q&A", theme=gr.themes.Soft()) as demo:
|
| 194 |
-
gr.Markdown("## 📚 Company Documents Q&A
|
| 195 |
|
| 196 |
with gr.Row():
|
| 197 |
with gr.Column(scale=3):
|
|
@@ -205,11 +207,11 @@ with gr.Blocks(title="Company Documents Q&A", theme=gr.themes.Soft()) as demo:
|
|
| 205 |
|
| 206 |
with gr.Row():
|
| 207 |
with gr.Column():
|
| 208 |
-
|
| 209 |
conf_out = gr.Markdown(label="Confidence")
|
| 210 |
source_out = gr.Markdown(label="Source Link")
|
| 211 |
-
record_out = gr.Markdown(label="Salesforce Record ID")
|
| 212 |
|
| 213 |
-
submit_btn.click(fn=process_question, inputs=question, outputs=[
|
| 214 |
|
| 215 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import logging
|
| 3 |
+
import os
|
| 4 |
import numpy as np
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
import faiss
|
| 8 |
from simple_salesforce import Salesforce
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
import json
|
| 11 |
+
import zipfile
|
| 12 |
+
from pathlib import Path
|
| 13 |
|
| 14 |
# Setup logging
|
| 15 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 133 |
except Exception as e:
|
| 134 |
# Log any error during record creation
|
| 135 |
logger.error(f"Error creating Salesforce record: {str(e)}")
|
|
|
|
|
|
|
| 136 |
return None
|
| 137 |
|
| 138 |
# --- Search & Answer ---
|
|
|
|
| 181 |
logger.error(f"Error in answer_query: {str(e)}")
|
| 182 |
return f"Error: {str(e)}", "", "", ""
|
| 183 |
|
| 184 |
+
# --- Gradio Chatbot UI ---
|
| 185 |
+
def process_question(q, chat_history):
|
| 186 |
if not q.strip():
|
| 187 |
+
return chat_history + [("User", "Please enter a question.")], "", ""
|
| 188 |
+
|
| 189 |
+
answer, confidence, source, record_id = answer_query(q)
|
| 190 |
+
chat_history.append(("User", q))
|
| 191 |
+
chat_history.append(("Bot", answer))
|
| 192 |
+
|
| 193 |
+
return chat_history, confidence, source, record_id
|
| 194 |
|
| 195 |
with gr.Blocks(title="Company Documents Q&A", theme=gr.themes.Soft()) as demo:
|
| 196 |
+
gr.Markdown("## 📚 Company Documents Q&A Chatbot")
|
| 197 |
|
| 198 |
with gr.Row():
|
| 199 |
with gr.Column(scale=3):
|
|
|
|
| 207 |
|
| 208 |
with gr.Row():
|
| 209 |
with gr.Column():
|
| 210 |
+
chat_history = gr.Chatbot(label="Chat History").style(height=400)
|
| 211 |
conf_out = gr.Markdown(label="Confidence")
|
| 212 |
source_out = gr.Markdown(label="Source Link")
|
| 213 |
+
record_out = gr.Markdown(label="Salesforce Record ID")
|
| 214 |
|
| 215 |
+
submit_btn.click(fn=process_question, inputs=[question, chat_history], outputs=[chat_history, conf_out, source_out, record_out])
|
| 216 |
|
| 217 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|