k-mktr commited on
Commit
15b10ef
·
verified ·
1 Parent(s): f715d34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -5
app.py CHANGED
@@ -23,8 +23,11 @@ from model_suggestions import add_suggestion, get_suggestions_html
23
  from release_notes import get_release_notes_html
24
 
25
 
26
- # Initialize logging for errors only
27
- logging.basicConfig(level=logging.ERROR)
 
 
 
28
  logger = logging.getLogger(__name__)
29
 
30
  # Start the backup thread
@@ -53,6 +56,7 @@ def call_ollama_api(model, prompt):
53
  )
54
 
55
  try:
 
56
  response = client.chat.completions.create(
57
  model=model,
58
  messages=[
@@ -67,13 +71,40 @@ def call_ollama_api(model, prompt):
67
  ],
68
  timeout=180
69
  )
70
- return response.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  except requests.exceptions.Timeout:
72
  logger.error(f"Timeout error for model {model} after 180 seconds")
73
  return f"Error: Model response timed out after 180 seconds"
74
  except Exception as e:
75
- logger.error(f"Error calling Ollama API for model {model}: {str(e)}")
76
- return f"Error: Unable to get response from the model."
77
 
78
  # Generate responses using two randomly selected models
79
  def get_battle_counts():
 
23
  from release_notes import get_release_notes_html
24
 
25
 
26
+ # Initialize logging for errors and info
27
+ logging.basicConfig(
28
+ level=logging.INFO,
29
+ format='%(asctime)s - %(levelname)s - %(message)s'
30
+ )
31
  logger = logging.getLogger(__name__)
32
 
33
  # Start the backup thread
 
56
  )
57
 
58
  try:
59
+ logger.info(f"Starting API call for model {model}")
60
  response = client.chat.completions.create(
61
  model=model,
62
  messages=[
 
71
  ],
72
  timeout=180
73
  )
74
+ logger.info(f"Received response from model {model}")
75
+
76
+ if not response or not response.choices:
77
+ logger.error(f"Empty response received from model {model}")
78
+ return f"Error: Empty response from the model"
79
+
80
+ content = response.choices[0].message.content
81
+ if not content:
82
+ logger.error(f"Empty content received from model {model}")
83
+ return f"Error: Empty content from the model"
84
+
85
+ # Log the raw content for debugging
86
+ logger.info(f"Raw content from {model}: {content[:200]}...") # Log first 200 chars
87
+
88
+ # Remove thinking tags and their content
89
+ import re
90
+ content = re.sub(r'<thinking>.*?</thinking>', '', content, flags=re.DOTALL)
91
+
92
+ # Clean up any double newlines that might be left
93
+ content = re.sub(r'\n\s*\n', '\n', content.strip())
94
+
95
+ if not content.strip():
96
+ logger.error(f"Content empty after removing thinking tags for model {model}")
97
+ return f"Error: Empty content after processing from the model"
98
+
99
+ logger.info(f"Successfully processed response from model {model}")
100
+ return content
101
+
102
  except requests.exceptions.Timeout:
103
  logger.error(f"Timeout error for model {model} after 180 seconds")
104
  return f"Error: Model response timed out after 180 seconds"
105
  except Exception as e:
106
+ logger.error(f"Error calling Ollama API for model {model}: {str(e)}", exc_info=True)
107
+ return f"Error: Unable to get response from the model. Error: {str(e)}"
108
 
109
  # Generate responses using two randomly selected models
110
  def get_battle_counts():