alpata commited on
Commit
e20dbf5
·
verified ·
1 Parent(s): c73db86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -67
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
- OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
7
-
8
- def process_files(xml_file, tcpou_file):
9
- try:
10
- with open(xml_file, 'r') as f:
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
- if __name__ == "__main__":
69
- if not OPENAI_API_KEY:
70
- gr.Error("OpenAI API key not configured! Add it in Space Settings → Secrets")
71
-
72
- openai.api_key = OPENAI_API_KEY
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()