Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,14 +8,21 @@ 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
|
|
|
|
| 19 |
|
| 20 |
def extract_text_from_pdf(self, pdf_file):
|
| 21 |
if not pdf_file:
|
|
@@ -41,12 +48,10 @@ class PDFChat:
|
|
| 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)
|
|
@@ -63,15 +68,15 @@ class PDFChat:
|
|
| 63 |
return "", None
|
| 64 |
|
| 65 |
structured_prompt = """
|
| 66 |
-
|
| 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 |
-
"
|
| 74 |
-
"
|
| 75 |
...
|
| 76 |
},
|
| 77 |
...
|
|
@@ -100,7 +105,6 @@ class PDFChat:
|
|
| 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:
|
|
@@ -119,7 +123,7 @@ class PDFChat:
|
|
| 119 |
pdf_chat = PDFChat()
|
| 120 |
|
| 121 |
with gr.Blocks() as demo:
|
| 122 |
-
gr.Markdown("#
|
| 123 |
|
| 124 |
with gr.Row():
|
| 125 |
with gr.Column(scale=1):
|
|
@@ -139,9 +143,10 @@ with gr.Blocks() as demo:
|
|
| 139 |
with gr.Column(scale=2):
|
| 140 |
gr.Markdown("### Microcontroller Selection Interface")
|
| 141 |
question_input = gr.Textbox(
|
| 142 |
-
label="
|
| 143 |
-
placeholder="
|
| 144 |
-
lines=3
|
|
|
|
| 145 |
)
|
| 146 |
explanation_text = gr.Textbox(
|
| 147 |
label="Explanation",
|
|
@@ -158,19 +163,21 @@ with gr.Blocks() as demo:
|
|
| 158 |
clear_history_button = gr.Button("Clear Chat History")
|
| 159 |
|
| 160 |
with gr.Group():
|
| 161 |
-
gr.Markdown("### Example
|
| 162 |
gr.Examples(
|
| 163 |
examples=[
|
| 164 |
-
["
|
| 165 |
-
["
|
| 166 |
-
["
|
| 167 |
-
["
|
| 168 |
],
|
| 169 |
inputs=[question_input],
|
| 170 |
-
label="Example
|
| 171 |
)
|
| 172 |
|
| 173 |
-
|
|
|
|
|
|
|
| 174 |
load_button.click(
|
| 175 |
pdf_chat.extract_text_from_pdf,
|
| 176 |
inputs=[pdf_input],
|
|
@@ -187,10 +194,6 @@ with gr.Blocks() as demo:
|
|
| 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],
|
|
|
|
| 8 |
import os
|
| 9 |
|
| 10 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
|
| 12 |
+
if not openai.api_key:
|
| 13 |
+
try:
|
| 14 |
+
openai.api_key = OPENAI_API_KEY
|
| 15 |
+
except NameError:
|
| 16 |
+
print("API key is not set in the environment or as a variable.")
|
| 17 |
+
|
| 18 |
class PDFChat:
|
| 19 |
def __init__(self):
|
| 20 |
self.pdf_text = ""
|
| 21 |
self.chat_history = []
|
| 22 |
+
self.system_prompt = """You are a knowledgeable assistant specializing in microcontrollers from various manufacturers including but not limited to Renesas, Texas Instruments (TI), and STMicroelectronics (STM).
|
| 23 |
When comparing microcontrollers, always provide structured data in a JSON format that can be converted to a table.
|
| 24 |
+
Focus on key specifications like CPU frequency, memory, peripherals, ADC Resolution, Flash Memory, temperature range, and special features.
|
| 25 |
+
Consider all manufacturers' products when making recommendations based on application requirements."""
|
| 26 |
|
| 27 |
def extract_text_from_pdf(self, pdf_file):
|
| 28 |
if not pdf_file:
|
|
|
|
| 48 |
|
| 49 |
def extract_json_from_text(self, text):
|
| 50 |
"""Extract JSON data from the response text"""
|
|
|
|
| 51 |
json_match = re.search(r'```json\s*(.*?)\s*```', text, re.DOTALL)
|
| 52 |
if json_match:
|
| 53 |
json_str = json_match.group(1)
|
| 54 |
else:
|
|
|
|
| 55 |
json_match = re.search(r'({[\s\S]*})', text)
|
| 56 |
if json_match:
|
| 57 |
json_str = json_match.group(1)
|
|
|
|
| 68 |
return "", None
|
| 69 |
|
| 70 |
structured_prompt = """
|
| 71 |
+
Based on the application requirements, recommend suitable microcontrollers and
|
| 72 |
provide your response in the following JSON format wrapped in ```json ```:
|
| 73 |
{
|
| 74 |
"explanation": "Your textual explanation here",
|
| 75 |
"comparison_table": [
|
| 76 |
{
|
| 77 |
"Feature": "feature name",
|
| 78 |
+
"Option1": "value",
|
| 79 |
+
"Option2": "value",
|
| 80 |
...
|
| 81 |
},
|
| 82 |
...
|
|
|
|
| 105 |
)
|
| 106 |
response_text = response.choices[0].message['content']
|
| 107 |
|
|
|
|
| 108 |
json_data = self.extract_json_from_text(response_text)
|
| 109 |
|
| 110 |
if json_data and "comparison_table" in json_data:
|
|
|
|
| 123 |
pdf_chat = PDFChat()
|
| 124 |
|
| 125 |
with gr.Blocks() as demo:
|
| 126 |
+
gr.Markdown("# Renesas Chatbot")
|
| 127 |
|
| 128 |
with gr.Row():
|
| 129 |
with gr.Column(scale=1):
|
|
|
|
| 143 |
with gr.Column(scale=2):
|
| 144 |
gr.Markdown("### Microcontroller Selection Interface")
|
| 145 |
question_input = gr.Textbox(
|
| 146 |
+
label="Briefly describe your target application for controller recommendation",
|
| 147 |
+
placeholder="Example: Industrial motor control system with precise temperature monitoring...",
|
| 148 |
+
lines=3,
|
| 149 |
+
value="" # This will keep the input persistent
|
| 150 |
)
|
| 151 |
explanation_text = gr.Textbox(
|
| 152 |
label="Explanation",
|
|
|
|
| 163 |
clear_history_button = gr.Button("Clear Chat History")
|
| 164 |
|
| 165 |
with gr.Group():
|
| 166 |
+
gr.Markdown("### Example Applications")
|
| 167 |
gr.Examples(
|
| 168 |
examples=[
|
| 169 |
+
["Industrial automation system requiring precise motion control and multiple sensor inputs"],
|
| 170 |
+
["Battery-powered IoT device with wireless connectivity and low power requirements"],
|
| 171 |
+
["High-performance motor control application with real-time processing needs"],
|
| 172 |
+
["Smart building management system with multiple environmental sensors"],
|
| 173 |
],
|
| 174 |
inputs=[question_input],
|
| 175 |
+
label="Example Applications"
|
| 176 |
)
|
| 177 |
|
| 178 |
+
def handle_question(question):
|
| 179 |
+
explanation, df = pdf_chat.answer_question(question)
|
| 180 |
+
return explanation, df, question
|
| 181 |
load_button.click(
|
| 182 |
pdf_chat.extract_text_from_pdf,
|
| 183 |
inputs=[pdf_input],
|
|
|
|
| 194 |
outputs=[explanation_text, table_output]
|
| 195 |
)
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
question_input.submit(
|
| 198 |
handle_question,
|
| 199 |
inputs=[question_input],
|