Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,22 +2,27 @@ import gradio as gr
|
|
| 2 |
import PyPDF2
|
| 3 |
import openai
|
| 4 |
from config import OPENAI_API_KEY
|
|
|
|
|
|
|
|
|
|
| 5 |
import os
|
| 6 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
-
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
class PDFChat:
|
| 11 |
def __init__(self):
|
| 12 |
self.pdf_text = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def extract_text_from_pdf(self, pdf_file):
|
| 15 |
-
"""Extract text from PDF file and store it"""
|
| 16 |
if not pdf_file:
|
| 17 |
return "Please upload a PDF file first."
|
| 18 |
|
| 19 |
try:
|
| 20 |
-
self.pdf_text = ""
|
| 21 |
with open(pdf_file.name, "rb") as file:
|
| 22 |
reader = PyPDF2.PdfReader(file)
|
| 23 |
for page in reader.pages:
|
|
@@ -25,27 +30,67 @@ class PDFChat:
|
|
| 25 |
return "PDF loaded successfully! You can now ask questions."
|
| 26 |
except Exception as e:
|
| 27 |
return f"Error loading PDF: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
"""
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
|
|
|
| 34 |
if not question:
|
| 35 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
# Construct the conversation context
|
| 38 |
messages = [
|
| 39 |
-
{"role": "system", "content":
|
| 40 |
-
{"role": "system", "content":
|
| 41 |
]
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
messages.append({"role": "user", "content": human})
|
| 46 |
messages.append({"role": "assistant", "content": assistant})
|
| 47 |
|
| 48 |
-
# Add current question
|
| 49 |
messages.append({"role": "user", "content": question})
|
| 50 |
|
| 51 |
try:
|
|
@@ -53,112 +98,110 @@ class PDFChat:
|
|
| 53 |
model="gpt-4-turbo",
|
| 54 |
messages=messages
|
| 55 |
)
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
except Exception as e:
|
| 62 |
error_message = f"Error generating response: {str(e)}"
|
| 63 |
-
|
| 64 |
-
return chat_history
|
| 65 |
-
|
| 66 |
-
def clear_history(self):
|
| 67 |
-
"""Clear conversation history"""
|
| 68 |
-
return []
|
| 69 |
|
| 70 |
-
css = """
|
| 71 |
-
.container {
|
| 72 |
-
max-width: 800px;
|
| 73 |
-
margin: auto;
|
| 74 |
-
}
|
| 75 |
-
.chat-window {
|
| 76 |
-
height: 600px;
|
| 77 |
-
overflow-y: auto;
|
| 78 |
-
}
|
| 79 |
-
"""
|
| 80 |
-
|
| 81 |
-
# Create PDF Chat instance
|
| 82 |
pdf_chat = PDFChat()
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
gr.Markdown("# Renesas PDF Chatbot")
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
-
with gr.Column(scale=
|
|
|
|
| 90 |
pdf_input = gr.File(
|
| 91 |
label="Upload PDF",
|
| 92 |
file_types=[".pdf"]
|
| 93 |
)
|
| 94 |
-
|
|
|
|
|
|
|
| 95 |
status_text = gr.Textbox(
|
| 96 |
label="Status",
|
| 97 |
interactive=False
|
| 98 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
chatbot = gr.Chatbot(
|
| 102 |
-
[],
|
| 103 |
-
elem_id="chatbot",
|
| 104 |
-
label="Chat History",
|
| 105 |
-
height=400
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
with gr.Row():
|
| 109 |
-
question_input = gr.Textbox(
|
| 110 |
-
label="Ask a question",
|
| 111 |
-
placeholder="What would you like to know about the PDF?",
|
| 112 |
-
scale=4
|
| 113 |
-
)
|
| 114 |
-
submit_button = gr.Button("Send", scale=1)
|
| 115 |
-
clear_button = gr.Button("Clear History", scale=1)
|
| 116 |
-
|
| 117 |
-
# Example queries
|
| 118 |
-
gr.Examples(
|
| 119 |
-
examples=[
|
| 120 |
-
["renesas-ra6m1-group-datasheet.pdf", "Which Renesas products are mentioned in this PDF?"],
|
| 121 |
-
["renesas-ra6m1-group-datasheet.pdf", "What are the key features of the microcontroller?"],
|
| 122 |
-
["renesas-ra6m1-group-datasheet.pdf", "Explain the power consumption specifications."]
|
| 123 |
-
],
|
| 124 |
-
inputs=[pdf_input, question_input],
|
| 125 |
-
label="Example Queries"
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
-
# Event handlers
|
| 129 |
load_button.click(
|
| 130 |
pdf_chat.extract_text_from_pdf,
|
| 131 |
inputs=[pdf_input],
|
| 132 |
outputs=[status_text]
|
| 133 |
)
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
| 138 |
|
| 139 |
-
|
| 140 |
-
pdf_chat.
|
| 141 |
-
|
| 142 |
-
outputs=[chatbot]
|
| 143 |
-
).then(
|
| 144 |
-
clear_input,
|
| 145 |
-
outputs=[question_input]
|
| 146 |
)
|
| 147 |
|
| 148 |
-
|
| 149 |
-
pdf_chat.answer_question
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
| 155 |
)
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
| 160 |
)
|
| 161 |
|
| 162 |
-
# Launch the interface with sharing enabled
|
| 163 |
if __name__ == "__main__":
|
| 164 |
demo.launch(debug=True)
|
|
|
|
| 2 |
import PyPDF2
|
| 3 |
import openai
|
| 4 |
from config import OPENAI_API_KEY
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import json
|
| 7 |
+
import re
|
| 8 |
import os
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 11 |
|
| 12 |
class PDFChat:
|
| 13 |
def __init__(self):
|
| 14 |
self.pdf_text = ""
|
| 15 |
+
self.chat_history = []
|
| 16 |
+
self.system_prompt = """You are a knowledgeable assistant specializing in microcontrollers from Renesas, TI, and STM.
|
| 17 |
+
When comparing microcontrollers, always provide structured data in a JSON format that can be converted to a table.
|
| 18 |
+
Focus on key specifications like CPU frequency, memory, peripherals, ADC Resolution , Flash Memory ,temperature range, and special features."""
|
| 19 |
|
| 20 |
def extract_text_from_pdf(self, pdf_file):
|
|
|
|
| 21 |
if not pdf_file:
|
| 22 |
return "Please upload a PDF file first."
|
| 23 |
|
| 24 |
try:
|
| 25 |
+
self.pdf_text = ""
|
| 26 |
with open(pdf_file.name, "rb") as file:
|
| 27 |
reader = PyPDF2.PdfReader(file)
|
| 28 |
for page in reader.pages:
|
|
|
|
| 30 |
return "PDF loaded successfully! You can now ask questions."
|
| 31 |
except Exception as e:
|
| 32 |
return f"Error loading PDF: {str(e)}"
|
| 33 |
+
|
| 34 |
+
def clear_pdf(self):
|
| 35 |
+
self.pdf_text = ""
|
| 36 |
+
return "PDF content cleared."
|
| 37 |
+
|
| 38 |
+
def clear_chat_history(self):
|
| 39 |
+
self.chat_history = []
|
| 40 |
+
return "", None
|
| 41 |
|
| 42 |
+
def extract_json_from_text(self, text):
|
| 43 |
+
"""Extract JSON data from the response text"""
|
| 44 |
+
# Find JSON pattern between ```json and ```
|
| 45 |
+
json_match = re.search(r'```json\s*(.*?)\s*```', text, re.DOTALL)
|
| 46 |
+
if json_match:
|
| 47 |
+
json_str = json_match.group(1)
|
| 48 |
+
else:
|
| 49 |
+
# Try to find JSON pattern between { and }
|
| 50 |
+
json_match = re.search(r'({[\s\S]*})', text)
|
| 51 |
+
if json_match:
|
| 52 |
+
json_str = json_match.group(1)
|
| 53 |
+
else:
|
| 54 |
+
return None
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
return json.loads(json_str)
|
| 58 |
+
except json.JSONDecodeError:
|
| 59 |
+
return None
|
| 60 |
|
| 61 |
+
def answer_question(self, question):
|
| 62 |
if not question:
|
| 63 |
+
return "", None
|
| 64 |
+
|
| 65 |
+
structured_prompt = """
|
| 66 |
+
If the question is asking for a comparison or suggestion of microcontrollers,
|
| 67 |
+
provide your response in the following JSON format wrapped in ```json ```:
|
| 68 |
+
{
|
| 69 |
+
"explanation": "Your textual explanation here",
|
| 70 |
+
"comparison_table": [
|
| 71 |
+
{
|
| 72 |
+
"Feature": "feature name",
|
| 73 |
+
"Controller1_Name": "value",
|
| 74 |
+
"Controller2_Name": "value",
|
| 75 |
+
...
|
| 76 |
+
},
|
| 77 |
+
...
|
| 78 |
+
]
|
| 79 |
+
}
|
| 80 |
+
"""
|
| 81 |
|
|
|
|
| 82 |
messages = [
|
| 83 |
+
{"role": "system", "content": self.system_prompt},
|
| 84 |
+
{"role": "system", "content": structured_prompt}
|
| 85 |
]
|
| 86 |
|
| 87 |
+
if self.pdf_text:
|
| 88 |
+
messages.append({"role": "system", "content": f"PDF Content: {self.pdf_text}"})
|
| 89 |
+
|
| 90 |
+
for human, assistant in self.chat_history:
|
| 91 |
messages.append({"role": "user", "content": human})
|
| 92 |
messages.append({"role": "assistant", "content": assistant})
|
| 93 |
|
|
|
|
| 94 |
messages.append({"role": "user", "content": question})
|
| 95 |
|
| 96 |
try:
|
|
|
|
| 98 |
model="gpt-4-turbo",
|
| 99 |
messages=messages
|
| 100 |
)
|
| 101 |
+
response_text = response.choices[0].message['content']
|
| 102 |
+
|
| 103 |
|
| 104 |
+
json_data = self.extract_json_from_text(response_text)
|
| 105 |
+
|
| 106 |
+
if json_data and "comparison_table" in json_data:
|
| 107 |
+
df = pd.DataFrame(json_data["comparison_table"])
|
| 108 |
+
explanation = json_data.get('explanation', response_text)
|
| 109 |
+
self.chat_history.append((question, explanation))
|
| 110 |
+
return explanation, df
|
| 111 |
+
else:
|
| 112 |
+
self.chat_history.append((question, response_text))
|
| 113 |
+
return response_text, None
|
| 114 |
+
|
| 115 |
except Exception as e:
|
| 116 |
error_message = f"Error generating response: {str(e)}"
|
| 117 |
+
return error_message, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
pdf_chat = PDFChat()
|
| 120 |
|
| 121 |
+
with gr.Blocks() as demo:
|
| 122 |
+
gr.Markdown("# Renasus Chatbot")
|
|
|
|
| 123 |
|
| 124 |
with gr.Row():
|
| 125 |
+
with gr.Column(scale=1):
|
| 126 |
+
gr.Markdown("### PDF Controls")
|
| 127 |
pdf_input = gr.File(
|
| 128 |
label="Upload PDF",
|
| 129 |
file_types=[".pdf"]
|
| 130 |
)
|
| 131 |
+
with gr.Row():
|
| 132 |
+
load_button = gr.Button("Load PDF")
|
| 133 |
+
clear_pdf_button = gr.Button("Clear PDF")
|
| 134 |
status_text = gr.Textbox(
|
| 135 |
label="Status",
|
| 136 |
interactive=False
|
| 137 |
)
|
| 138 |
+
|
| 139 |
+
with gr.Column(scale=2):
|
| 140 |
+
gr.Markdown("### Microcontroller Selection Interface")
|
| 141 |
+
question_input = gr.Textbox(
|
| 142 |
+
label="Ask about microcontroller selection",
|
| 143 |
+
placeholder="Describe your requirements or ask for comparisons...",
|
| 144 |
+
lines=3
|
| 145 |
+
)
|
| 146 |
+
explanation_text = gr.Textbox(
|
| 147 |
+
label="Explanation",
|
| 148 |
+
interactive=False,
|
| 149 |
+
lines=4
|
| 150 |
+
)
|
| 151 |
+
table_output = gr.DataFrame(
|
| 152 |
+
label="Comparison Table",
|
| 153 |
+
interactive=False,
|
| 154 |
+
wrap=True
|
| 155 |
+
)
|
| 156 |
+
with gr.Row():
|
| 157 |
+
submit_button = gr.Button("Send")
|
| 158 |
+
clear_history_button = gr.Button("Clear Chat History")
|
| 159 |
+
|
| 160 |
+
with gr.Group():
|
| 161 |
+
gr.Markdown("### Example Questions")
|
| 162 |
+
gr.Examples(
|
| 163 |
+
examples=[
|
| 164 |
+
["Suggest controller suitable for water level monitoring system comparing RA4M1 and STM32L4"],
|
| 165 |
+
["Recommend controller for centralized vehicle lighting and door control systems comparing RA6M3 and STM32F4"],
|
| 166 |
+
["Suggest best suited controller for a Solar Inverter Design comparing RA6T1 and TMS320F28379D"],
|
| 167 |
+
["Compare RA6M5 and STM32G4 series for building automation applications"],
|
| 168 |
+
],
|
| 169 |
+
inputs=[question_input],
|
| 170 |
+
label="Example Questions"
|
| 171 |
+
)
|
| 172 |
|
| 173 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
load_button.click(
|
| 175 |
pdf_chat.extract_text_from_pdf,
|
| 176 |
inputs=[pdf_input],
|
| 177 |
outputs=[status_text]
|
| 178 |
)
|
| 179 |
|
| 180 |
+
clear_pdf_button.click(
|
| 181 |
+
pdf_chat.clear_pdf,
|
| 182 |
+
outputs=[status_text]
|
| 183 |
+
)
|
| 184 |
|
| 185 |
+
clear_history_button.click(
|
| 186 |
+
pdf_chat.clear_chat_history,
|
| 187 |
+
outputs=[explanation_text, table_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
)
|
| 189 |
|
| 190 |
+
def handle_question(question):
|
| 191 |
+
explanation, df = pdf_chat.answer_question(question)
|
| 192 |
+
return explanation, df, ""
|
| 193 |
+
|
| 194 |
+
question_input.submit(
|
| 195 |
+
handle_question,
|
| 196 |
+
inputs=[question_input],
|
| 197 |
+
outputs=[explanation_text, table_output, question_input]
|
| 198 |
)
|
| 199 |
|
| 200 |
+
submit_button.click(
|
| 201 |
+
handle_question,
|
| 202 |
+
inputs=[question_input],
|
| 203 |
+
outputs=[explanation_text, table_output, question_input]
|
| 204 |
)
|
| 205 |
|
|
|
|
| 206 |
if __name__ == "__main__":
|
| 207 |
demo.launch(debug=True)
|