Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,17 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
-
from tempfile import NamedTemporaryFile
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
xml_content = f.read()
|
| 12 |
-
|
| 13 |
-
with open(tcpou_file, 'r') as f:
|
| 14 |
-
tcpou_content = f.read()
|
| 15 |
-
|
| 16 |
-
except Exception as e:
|
| 17 |
-
return f"Error reading files: {str(e)}", None
|
| 18 |
-
|
| 19 |
-
prompt = f"""Analyze these PLC development files:
|
| 20 |
-
|
| 21 |
-
XML File Content:
|
| 22 |
-
{xml_content}
|
| 23 |
-
|
| 24 |
-
TcPOU Code File Content:
|
| 25 |
-
{tcpou_content}
|
| 26 |
-
|
| 27 |
-
Provide a detailed analysis of the code structure, functionality,
|
| 28 |
-
and any potential issues or improvements. Also include documentation suggestions.
|
| 29 |
-
"""
|
| 30 |
-
|
| 31 |
-
try:
|
| 32 |
-
response = openai.ChatCompletion.create(
|
| 33 |
-
model="gpt-3.5-turbo",
|
| 34 |
-
messages=[
|
| 35 |
-
{"role": "system", "content": "You are an industrial automation expert analyzing PLC code."},
|
| 36 |
-
{"role": "user", "content": prompt}
|
| 37 |
-
]
|
| 38 |
-
)
|
| 39 |
-
analysis = response.choices[0].message.content
|
| 40 |
-
except Exception as e:
|
| 41 |
-
return f"API Error: {str(e)}", None
|
| 42 |
-
|
| 43 |
-
with NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as f:
|
| 44 |
-
f.write(analysis)
|
| 45 |
-
temp_path = f.name
|
| 46 |
-
|
| 47 |
-
return analysis, temp_path
|
| 48 |
-
|
| 49 |
-
with gr.Blocks() as demo:
|
| 50 |
-
gr.Markdown("# PLC Code Analyzer 🤖")
|
| 51 |
-
|
| 52 |
-
with gr.Row():
|
| 53 |
-
with gr.Column():
|
| 54 |
-
xml_input = gr.File(label="Upload XML File", type="filepath")
|
| 55 |
-
tcpou_input = gr.File(label="Upload TcPOU File", type="filepath")
|
| 56 |
-
submit_btn = gr.Button("Analyze Files")
|
| 57 |
-
|
| 58 |
-
with gr.Column():
|
| 59 |
-
output_text = gr.Textbox(label="Analysis Result", interactive=False)
|
| 60 |
-
download_output = gr.File(label="Download Result", interactive=False)
|
| 61 |
-
|
| 62 |
-
submit_btn.click(
|
| 63 |
-
fn=process_files,
|
| 64 |
-
inputs=[xml_input, tcpou_input],
|
| 65 |
-
outputs=[output_text, download_output]
|
| 66 |
)
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
demo.launch(concurrency_count=20) # Corrected line
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
+
def analyze(xml, code):
|
| 6 |
+
prompt = f"XML: {xml}\nCode: {code}\nAnalyze:"
|
| 7 |
+
response = openai.ChatCompletion.create(
|
| 8 |
+
model="gpt-3.5-turbo",
|
| 9 |
+
messages=[{"role": "user", "content": prompt}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
)
|
| 11 |
+
return response.choices[0].message.content
|
| 12 |
|
| 13 |
+
gr.Interface(
|
| 14 |
+
fn=analyze,
|
| 15 |
+
inputs=[gr.File(), gr.File()],
|
| 16 |
+
outputs=[gr.Textbox(), gr.File()]
|
| 17 |
+
).launch()
|
|
|