Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,23 @@ def main_loop(self): #FUTURE -> Refactor this to its own class
"stream": False # don't really know why --> look into this later.
}

response = requests.post(ollama_url, json=payload)
try:
response = requests.post(ollama_url, json=payload, timeout=30)
response.raise_for_status()
except requests.exceptions.RequestException as e:
raise RuntimeError(f"Failed to reach Ollama at {ollama_url}: {e}") from e

try:
json_data = response.json()
except ValueError as e:
raise RuntimeError(f"Ollama returned non-JSON response from {ollama_url}") from e

if "response" not in json_data:
raise RuntimeError(
f"Ollama response missing 'response' key. Payload keys: {list(json_data.keys())}"
)

# parse response
json_data = response.json()
parsed_response = json_data['response']
# print(parsed_response)
parsed_response = json_data["response"]
self.add_response_to_json(field, parsed_response)

print("----------------------------------")
Expand Down Expand Up @@ -130,7 +141,7 @@ class Fill():
def __init__(self):
pass

def fill_form(user_input: str, definitions: list, pdf_form: str):
def fill_form(self, user_input: str, definitions: list, pdf_form: str):
"""
Fill a PDF form with values from user_input using testToJSON.
Fields are filled in the visual order (top-to-bottom, left-to-right).
Expand All @@ -148,7 +159,11 @@ def fill_form(user_input: str, definitions: list, pdf_form: str):
pdf = PdfReader(pdf_form)

# Loop through pages
for page in pdf.pages:
pages = pdf.pages
if pages is None or not isinstance(pages, list):
raise ValueError(f"PDF file '{pdf_form}' could not be read or has no pages")

for page in pages:
if page.Annots:
sorted_annots = sorted(
page.Annots,
Expand Down
20 changes: 11 additions & 9 deletions src/test/test_model.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# test_ollama.py
import ollama
import pytest

try:
response = ollama.chat(model='mistral', messages=[
{'role': 'user', 'content': 'Say hello in Spanish'}
])
print("Success! Response:", response['message']['content'])
except Exception as e:
print("Error:", e)

def test_ollama_chat_smoke():
ollama = pytest.importorskip("ollama")
response = ollama.chat(
model="mistral",
messages=[{"role": "user", "content": "Say hello in Spanish"}],
)
assert "message" in response
assert "content" in response["message"]
assert response["message"]["content"]