Sw1ft0 commited on
Commit
8f502b4
·
1 Parent(s): e5581b1

Add lines filter to remove unwanted code from Gemini's output

Browse files
Files changed (1) hide show
  1. app.py +41 -30
app.py CHANGED
@@ -25,46 +25,57 @@ def answer_question(history, message):
25
  history: chat history (list of [user, assistant] pairs)
26
  message: latest user message (string)
27
  """
28
- # Build prompt for Gemini
29
  prompt = f"""
30
- You are a data analysis assistant.
31
- You can ONLY answer questions using the two Excel reports provided (df1 and df2).
32
- Do not hallucinate or use external knowledge.
33
- If the question is irrelevant, respond with:
34
- "I can only answer questions about the provided Excel reports."
35
-
36
- The reports have the following schema:
37
- {schema_info}
38
-
39
- The user asked:
40
- {message}
41
-
42
- Rules:
43
- - Use only pandas, df1, df2, and Python built-ins.
44
- - Do NOT import anything (numpy, pandas, etc. are already available).
45
- - Always put the result in a variable named `result`.
46
-
47
- Generate Python code that computes the answer.
48
- Return ONLY code, nothing else.
49
  """
50
 
51
  try:
52
- # Call Gemini
53
  response = model.generate_content(prompt)
54
- code = response.text.strip("```python").strip("```")
55
-
56
- # 4. Execute code safely
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  local_vars = {"df1": df1, "df2": df2, "pd": pd}
58
- try:
59
- result = eval(code, {"__builtins__": {}}, local_vars)
60
- except Exception as e:
61
- exec(code, {"__builtins__": {}}, local_vars)
62
- result = local_vars.get("result", "No result variable found")
63
 
 
 
 
 
 
64
  return str(result)
65
 
66
  except Exception as e:
67
- return f"Error: {str(e)}"
68
 
69
 
70
  # 5. Gradio UI
 
25
  history: chat history (list of [user, assistant] pairs)
26
  message: latest user message (string)
27
  """
 
28
  prompt = f"""
29
+ You are a data analysis assistant.
30
+ You can ONLY answer questions using the two Excel reports provided (df1 and df2).
31
+ Do not hallucinate or use external knowledge.
32
+ If the question is irrelevant, respond with:
33
+ "I can only answer questions about the provided Excel reports."
34
+
35
+ The reports have the following schema:
36
+ {schema_info}
37
+
38
+ The user asked:
39
+ {message}
40
+
41
+ Rules:
42
+ - Use only pandas, df1, df2, and Python built-ins.
43
+ - Do NOT write import statements (pandas is already imported as pd).
44
+ - Always put the answer in a variable named `result`.
45
+ - Return ONLY Python code, nothing else.
 
 
46
  """
47
 
48
  try:
49
+ # Ask Gemini
50
  response = model.generate_content(prompt)
51
+ code = response.text.strip()
52
+
53
+ # Strip markdown fences if any
54
+ if code.startswith("```"):
55
+ code = code.strip("```").replace("python", "").strip()
56
+
57
+ # Remove dangerous or irrelevant lines
58
+ safe_lines = []
59
+ for line in code.splitlines():
60
+ if line.strip().startswith("import"):
61
+ continue
62
+ if line.strip().startswith("!"): # e.g. shell commands
63
+ continue
64
+ safe_lines.append(line)
65
+ safe_code = "\n".join(safe_lines)
66
+
67
+ # Prepare sandbox
68
  local_vars = {"df1": df1, "df2": df2, "pd": pd}
 
 
 
 
 
69
 
70
+ # Execute code
71
+ exec(safe_code, {"__builtins__": {}}, local_vars)
72
+
73
+ # Fetch result
74
+ result = local_vars.get("result", "No result produced")
75
  return str(result)
76
 
77
  except Exception as e:
78
+ return f"Execution error: {e}"
79
 
80
 
81
  # 5. Gradio UI