Lasdw commited on
Commit
b5b8395
·
1 Parent(s): b4bcd4c

fixed input bug 2

Browse files
Files changed (2) hide show
  1. app.py +21 -12
  2. uploads/download.jpeg +0 -0
app.py CHANGED
@@ -35,12 +35,17 @@ def format_history_for_agent(history: list) -> str:
35
  """
36
  Format the chat history into a string that the agent can understand.
37
  """
38
- formatted_history = ""
 
 
 
39
  for message in history:
40
- role = message["role"]
41
- content = message["content"]
42
- formatted_history += f"{role.upper()}: {content}\n"
43
- return formatted_history
 
 
44
 
45
  def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
46
  """
@@ -81,12 +86,20 @@ def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
81
 
82
  # Check if file extension is allowed
83
  if file_ext in ALLOWED_FILE_EXTENSIONS:
 
 
 
 
 
 
 
 
84
  # Read file content and encode as base64
85
- with open(file_path, "rb") as f:
86
  file_content = f.read()
87
  file_content_b64 = base64.b64encode(file_content).decode("utf-8")
88
  attachments[file_name] = file_content_b64
89
- file_info += f"\nUploaded file: {file_name}"
90
 
91
  if file_info:
92
  if question.strip():
@@ -99,11 +112,7 @@ def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
99
  print(f"Current conversation history:\n{conversation_history}") # Debug print
100
 
101
  # Prepare the full context for the agent
102
- full_context = {
103
- "question": question,
104
- "conversation_history": conversation_history,
105
- "session_id": session_id
106
- }
107
 
108
  # Get response from agent with attachments if available
109
  if attachments:
 
35
  """
36
  Format the chat history into a string that the agent can understand.
37
  """
38
+ if not history:
39
+ return ""
40
+
41
+ formatted_history = []
42
  for message in history:
43
+ if isinstance(message, dict) and "role" in message and "content" in message:
44
+ role = message["role"]
45
+ content = message["content"]
46
+ formatted_history.append(f"{role.upper()}: {content}")
47
+
48
+ return "\n".join(formatted_history)
49
 
50
  def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
51
  """
 
86
 
87
  # Check if file extension is allowed
88
  if file_ext in ALLOWED_FILE_EXTENSIONS:
89
+ # Create a local copy of the file
90
+ local_path = os.path.join("uploads", file_name)
91
+ os.makedirs("uploads", exist_ok=True)
92
+
93
+ # Copy the file to local storage
94
+ with open(file_path, "rb") as src, open(local_path, "wb") as dst:
95
+ dst.write(src.read())
96
+
97
  # Read file content and encode as base64
98
+ with open(local_path, "rb") as f:
99
  file_content = f.read()
100
  file_content_b64 = base64.b64encode(file_content).decode("utf-8")
101
  attachments[file_name] = file_content_b64
102
+ file_info += f"\nUploaded file: {local_path}"
103
 
104
  if file_info:
105
  if question.strip():
 
112
  print(f"Current conversation history:\n{conversation_history}") # Debug print
113
 
114
  # Prepare the full context for the agent
115
+ full_context = f"Question: {question}\n\nConversation History:\n{conversation_history}"
 
 
 
 
116
 
117
  # Get response from agent with attachments if available
118
  if attachments:
uploads/download.jpeg ADDED