Spaces:
Runtime error
Runtime error
Download agent_utils.py from knvishwanath/PV-Defects-Cleaning: direct link, hf CLI and curl.
- Browser
- Download file 2.94 kB
-
https://huggingface.co/spaces/knvishwanath/PV-Defects-Cleaning/resolve/main/agent_utils.py
- Command line
-
hf download hf://spaces/knvishwanath/PV-Defects-Cleaning/agent_utils.py
-
curl -L -o agent_utils.py https://huggingface.co/spaces/knvishwanath/PV-Defects-Cleaning/resolve/main/agent_utils.py
2.94 kB
| import openai | |
| import streamlit as st | |
| import sqlite3 | |
| import pandas as pd | |
| import json | |
| DB_PATH = "pv_inspection.db" | |
| client = openai.OpenAI() | |
| def analyze_pdf(text, api_key): | |
| openai.api_key = api_key | |
| prompt = f"""You are a PV inspection assistant AI. | |
| Analyze the following report and return a JSON object like this: | |
| {{ | |
| "classification": "PASS / OBSERVATION / FAILURE", | |
| "severity": "X/10", | |
| "urgency": "Immediate / Routine / Low", | |
| "summary": [ | |
| {{"panel": "P-001", "defects": ["burn-mark"], "action": "Replace panel"}} | |
| ], | |
| "groups": {{ | |
| "PASS": ["P-005"], | |
| "OBSERVATION": ["P-006"], | |
| "FAILURE": ["P-001", "P-002"] | |
| }} | |
| }} | |
| Here is the report: | |
| {text} | |
| """ | |
| try: | |
| response = client.chat.completions.create( | |
| model="gpt-3.5-turbo", | |
| messages=[{"role": "user", "content": prompt}], | |
| temperature=0.3, | |
| max_tokens=1000 | |
| ) | |
| return json.loads(response.choices[0].message.content) | |
| except Exception as e: | |
| st.error(f"GPT error: {e}") | |
| return { | |
| "classification": "UNKNOWN", | |
| "severity": "-", | |
| "urgency": "-", | |
| "summary": [], | |
| "groups": {"PASS": [], "OBSERVATION": [], "FAILURE": []} | |
| } | |
| def run_chat_agent(context, api_key): | |
| openai.api_key = api_key | |
| st.markdown("## 💬 Chat with the Inspection Agent") | |
| if "chat_history" not in st.session_state: | |
| st.session_state.chat_history = [] | |
| user_input = st.text_input("Ask about the report or past inspections", key="agent_chat_input") | |
| if user_input: | |
| st.session_state.chat_history.append(("user", user_input)) | |
| with st.spinner("Thinking..."): | |
| try: | |
| messages = [{"role": "system", "content": f"You're a solar PV assistant. Report:\n{context}"}] | |
| for role, msg in st.session_state.chat_history: | |
| messages.append({"role": role, "content": msg}) | |
| with sqlite3.connect(DB_PATH) as conn: | |
| df = pd.read_sql_query("SELECT * FROM inspections", conn) | |
| defect_summary = df.groupby("defect").size().to_string() | |
| messages.append({"role": "user", "content": f"Historical defect data:\n{defect_summary}"}) | |
| response = client.chat.completions.create( | |
| model="gpt-3.5-turbo", | |
| messages=messages, | |
| temperature=0.4, | |
| max_tokens=700 | |
| ) | |
| answer = response.choices[0].message.content | |
| st.session_state.chat_history.append(("assistant", answer)) | |
| except Exception as e: | |
| st.error(f"Agent error: {e}") | |
| return | |
| for role, msg in st.session_state.chat_history: | |
| st.markdown(f"**{role.capitalize()}:** {msg}") | |
| if st.button("🔄 Reset Chat"): | |
| st.session_state.chat_history = [] | |