ModuleNotFoundError:ollama

I’m getting an error module not found in a simple st and ollama demo.
the github link: GitHub - ProfNuzen/streamlit
the https://share.streamlit.io/user/profnuzen
error message:
ModuleNotFoundError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you’re on Streamlit Cloud, click on ‘Manage app’ in the lower right of your app).

Traceback:

File "/mount/src/streamlit/st_llama.py", line 2, in <module>
    import ollama

requirements.txt
streamlit
ollama

Thank you.

There’s no requirements.txt file in your repo

Thank you.

Hi Sid, I now have another problem. It doesn’t appear that Ollama is running on the cloud. How do I start Ollama on Streamlit?
Thanks,
Ahn

httpx.ConnectError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you’re on Streamlit Cloud, click on ‘Manage app’ in the lower right of your app).

Traceback:

File "/mount/src/streamlit/st_llama.py", line 57, in <module>
    main()File "/mount/src/streamlit/st_llama.py", line 46, in main
    result = ollama.chat(model=chat_model, messages=[{
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py", line 332, in chat
    return self._request(
           ^^^^^^^^^^^^^^File "/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py", line 177, in _request
    return cls(**self._request_raw(*args, **kwargs).json())
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py", line 118, in _request_raw
    r = self._client.request(*args, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py", line 837, in request
    return self.send(request, auth=auth, follow_redirects=follow_redirects)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py", line 926, in send
    response = self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^File "/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py", line 954, in _send_handling_auth
    response = self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Could you share the full traceback from the app cloud console (Manage app at the lower right)?

I think the errors indicate that Ollama program is not running.
on my laptop I normally run ollama first before streamlit
1/ ollama run llama3
2/ streamlit run st_ollama.py

Is there a way to run ollama first before running my python script?

[ UTC ] Logs for profnuzen.streamlit.app/
────────────────────────────────────────────────────────────────────────────────────────
[04:47:46] :rocket: Starting up repository: ‘streamlit’, branch: ‘main’, main module: ‘st_llama.py’
[04:47:46] :octopus: Cloning repository…
[04:47:47] :octopus: Cloning into ‘/mount/src/streamlit’…

[04:47:47] :octopus: Cloned repository!
[04:47:47] :octopus: Pulling code changes from Github…
[04:47:48] :package: Processing dependencies…

──────────────────────────────────────── uv ───────────────────────────────────────────

Using uv pip install.
Using Python 3.12.8 environment at /home/adminuser/venv
Resolved 50 packages in 646ms
Prepared [2025-01-04 04:47:52.043993] 50 packages[2025-01-04 04:47:52.044889] [2025-01-04 04:47:52.045139] in 2.86s[2025-01-04 04:47:52.045339]
Installed 50 packages in 124ms

  • altair==5.5.0
  • annotated-types==0.7.0
  • anyio==4.7.0
  • attrs==24.3.0
  • blinker==1.9.0
  • cachetools==5.5.0
  • certifi==2024.12.14
  • charset-normalizer==3.4.1
  • click==8.1.8
  • gitdb==4.0.12
  • gitpython==3.1.44
  • h11==0.14.0
  • httpcore==1.0.7
  • httpx==0.27.2
  • idna==3.10
  • jinja2==3.1.5
  • jsonschema==4.23.0
  • jsonschema-specifications==2024.10.1
  • markdown-it-py==3.0.0
  • markupsafe==3.0.2
  • mdurl==0.1.2
  • narwhals==1.20.1
  • numpy==2.2.1
  • ollama==0.4.5
  • packaging==24.2
  • pandas==2.2.3
  • pillow==11.1.0
  • protobuf==5.29.2
  • pyarrow==18.1.0
  • pydantic==2.10.4
  • pydantic-core==2.27.2
  • pydeck==0.9.1
    [2025-01-04 04:47:52.170566] + pygments==2.18.0
  • python-dateutil==2.9.0.post0
  • pytz==2024.2
  • referencing==0.35.1
  • requests==2.32.3
  • rich==13.9.4
  • rpds-py==0.22.3
  • six==1.17.0
  • smmap==5.0.2
  • sniffio==1.3.1
  • streamlit==1.41.1
  • tenacity==9.0.0
  • [2025-01-04 04:47:52.170870] toml==0.10.2
  • tornado==6.4.2
  • typing-extensions==4.12.2
  • tzdata==2024.2
    +[2025-01-04 04:47:52.171113] urllib3==2.3.0
  • watchdog==6.0.0
    Checking if Streamlit is installed
    Found Streamlit version 1.41.1 in the environment

────────────────────────────────────────────────────────────────────────────────────────

[04:47:54] :snake: Python dependencies were installed from /mount/src/streamlit/requirements.txt using uv.
Check if streamlit is installed
Streamlit is already installed
[04:47:56] :package: Processed dependencies!

────────────────────── Traceback (most recent call last) ───────────────────────
/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.
py:72 in map_httpcore_exceptions

 69 @contextlib.contextmanager                                              
 70 def map_httpcore_exceptions() -> typing.Iterator[None]:                 
 71 │   try:                                                                

❱ 72 │ │ yield
73 │ except Exception as exc:
74 │ │ mapped_exc = None
75

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.
py:236 in handle_request

233 │   │   │   extensions=request.extensions,                              
234 │   │   )                                                               
235 │   │   with map_httpcore_exceptions():                                 

❱ 236 │ │ │ resp = self._pool.handle_request(req)
237 │ │
238 │ │ assert isinstance(resp.stream, typing.Iterable)
239

/home/adminuser/venv/lib/python3.12/site-packages/httpcore/sync/connection
pool.py:256 in handle_request

253 │   │   │   │   closing = self._assign_requests_to_connections()        
254 │   │   │                                                               
255 │   │   │   self._close_connections(closing)                            

❱ 256 │ │ │ raise exc from None
257 │ │
258 │ │ # Return the response. Note that in this case we still have to
259 │ │ # the point at which the response is closed.

/home/adminuser/venv/lib/python3.12/site-packages/httpcore/sync/connection
pool.py:236 in handle_request

233 │   │   │   │                                                           
234 │   │   │   │   try:                                                    
235 │   │   │   │   │   # Send the request on the assigned connection.      

❱ 236 │ │ │ │ │ response = connection.handle_request(
237 │ │ │ │ │ │ pool_request.request
238 │ │ │ │ │ )
239 │ │ │ │ except ConnectionNotAvailable:

/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection.
py:101 in handle_request

 98 │   │   │   │   │   │   )                                               
 99 │   │   except BaseException as exc:                                    
100 │   │   │   self._connect_failed = True                                 

❱ 101 │ │ │ raise exc
102 │ │
103 │ │ return self._connection.handle_request(request)
104

/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection.
py:78 in handle_request

 75 │   │   try:                                                            
 76 │   │   │   with self._request_lock:                                    
 77 │   │   │   │   if self._connection is None:                            

❱ 78 │ │ │ │ │ stream = self._connect(request)
79 │ │ │ │ │
80 │ │ │ │ │ ssl_object = stream.get_extra_info(“ssl_object”)
81 │ │ │ │ │ http2_negotiated = (

/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection.
py:124 in _connect

121 │   │   │   │   │   │   "socket_options": self._socket_options,         
122 │   │   │   │   │   }                                                   
123 │   │   │   │   │   with Trace("connect_tcp", logger, request, kwargs)  

❱ 124 │ │ │ │ │ │ stream = self._network_backend.connect_tcp(**k
125 │ │ │ │ │ │ trace.return_value = stream
126 │ │ │ │ else:
127 │ │ │ │ │ kwargs = {

/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_backends/sync.py
:207 in connect_tcp

204 │   │   │   OSError: ConnectError,                                      
205 │   │   }                                                               
206 │   │                                                                   

❱ 207 │ │ with map_exceptions(exc_map):
208 │ │ │ sock = socket.create_connection(
209 │ │ │ │ address,
210 │ │ │ │ timeout,

/usr/local/lib/python3.12/contextlib.py:158 in exit

155 │   │   │   │   # tell if we get the same exception back                
156 │   │   │   │   value = typ()                                           
157 │   │   │   try:                                                        

❱ 158 │ │ │ │ self.gen.throw(value)
159 │ │ │ except StopIteration as exc:
160 │ │ │ │ # Suppress StopIteration unless it’s the same except
161 │ │ │ │ # was passed to throw(). This prevents a StopIteratio

/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_exceptions.py:14
in map_exceptions

11 │   except Exception as exc:  # noqa: PIE786                             
12 │   │   for from_exc, to_exc in map.items():                             
13 │   │   │   if isinstance(exc, from_exc):                                

❱ 14 │ │ │ │ raise to_exc(exc) from exc
15 │ │ raise # pragma: nocover
16
17
────────────────────────────────────────────────────────────────────────────────
ConnectError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

────────────────────── Traceback (most recent call last) ───────────────────────
/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptru
nner/exec_code.py:88 in exec_func_with_error_handling

/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptru
nner/script_runner.py:579 in code_to_exec

/mount/src/streamlit/st_llama.py:57 in

54 │   │   │   st.write_stream(stream_lines(response))                      
55                                                                          
56 if __name__ == "__main__":                                               

❱ 57 │ main()
58

/mount/src/streamlit/st_llama.py:46 in main

43 │   │   # st.write(ollama.list())                                        
44 │   │   chat_model = "llama3"                                            
45 │   │   with st.spinner("Thinking ..."):                                 

❱ 46 │ │ │ result = ollama.chat(model=chat_model, messages=[{
47 │ │ │ │ “role”: “user”,
48 │ │ │ │ “content”: prompt,
49 │ │ │ }])

/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py:332 in
chat

 329 │                                                                      
 330 │   Returns `ChatResponse` if `stream` is `False`, otherwise returns   
 331 │   """                                                                

❱ 332 │ return self._request(
333 │ ChatResponse,
334 │ ‘POST’,
335 │ ‘/api/chat’,

/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py:177 in
_request

 174 │                                                                      
 175 │     return inner()                                                   
 176 │                                                                      

❱ 177 │ return cls(**self._request_raw(*args, **kwargs).json())
178
179 @overload
180 def generate(

/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py:118 in
_request_raw

 115 │   super().__init__(httpx.Client, host, **kwargs)                     
 116                                                                        
 117   def _request_raw(self, *args, **kwargs):                             

❱ 118 │ r = self._client.request(*args, **kwargs)
119 │ try:
120 │ r.raise_for_status()
121 │ except httpx.HTTPStatusError as e:

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py:837 in
request

 834 │   │   │   timeout=timeout,                                           
 835 │   │   │   extensions=extensions,                                     
 836 │   │   )                                                              

❱ 837 │ │ return self.send(request, auth=auth, follow_redirects=follow_
838 │
839 │ @contextmanager
840 │ def stream(

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py:926 in
send

 923 │   │                                                                  
 924 │   │   auth = self._build_request_auth(request, auth)                 
 925 │   │                                                                  

❱ 926 │ │ response = self._send_handling_auth(
927 │ │ │ request,
928 │ │ │ auth=auth,
929 │ │ │ follow_redirects=follow_redirects,

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py:954 in
_send_handling_auth

 951 │   │   │   request = next(auth_flow)                                  
 952 │   │   │                                                              
 953 │   │   │   while True:                                                

❱ 954 │ │ │ │ response = self._send_handling_redirects(
955 │ │ │ │ │ request,
956 │ │ │ │ │ follow_redirects=follow_redirects,
957 │ │ │ │ │ history=history,

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py:991 in
_send_handling_redirects

 988 │   │   │   for hook in self._event_hooks["request"]:                  
 989 │   │   │   │   hook(request)                                          
 990 │   │   │                                                              

❱ 991 │ │ │ response = self._send_single_request(request)
992 │ │ │ try:
993 │ │ │ │ for hook in self._event_hooks[“response”]:
994 │ │ │ │ │ hook(response)

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py:1027 in
_send_single_request

1024 │   │   │   )                                                          
1025 │   │                                                                  
1026 │   │   with request_context(request=request):                         

❱ 1027 │ │ │ response = transport.handle_request(request)
1028 │ │
1029 │ │ assert isinstance(response.stream, SyncByteStream)
1030

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.
py:235 in handle_request

232 │   │   │   content=request.stream,                                     
233 │   │   │   extensions=request.extensions,                              
234 │   │   )                                                               

❱ 235 │ │ with map_httpcore_exceptions():
236 │ │ │ resp = self._pool.handle_request(req)
237 │ │
238 │ │ assert isinstance(resp.stream, typing.Iterable)

/usr/local/lib/python3.12/contextlib.py:158 in exit

155 │   │   │   │   # tell if we get the same exception back                
156 │   │   │   │   value = typ()                                           
157 │   │   │   try:                                                        

❱ 158 │ │ │ │ self.gen.throw(value)
159 │ │ │ except StopIteration as exc:
160 │ │ │ │ # Suppress StopIteration unless it’s the same except
161 │ │ │ │ # was passed to throw(). This prevents a StopIteratio

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.
py:89 in map_httpcore_exceptions

 86 │   │   │   raise                                                       
 87 │   │                                                                   
 88 │   │   message = str(exc)                                              

❱ 89 │ │ raise mapped_exc(message) from exc
90
91
92 HTTPCORE_EXC_MAP = {
────────────────────────────────────────────────────────────────────────────────
ConnectError: [Errno 111] Connection refused
2025-01-04 04:50:12.550 Uncaught app execution
Traceback (most recent call last):
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.py”, line 72, in map_httpcore_exceptions
yield
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.py”, line 236, in handle_request
resp = self._pool.handle_request(req)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py”, line 256, in handle_request
raise exc from None
File “/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py”, line 236, in handle_request
response = connection.handle_request(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection.py”, line 101, in handle_request
raise exc
File “/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection.py”, line 78, in handle_request
stream = self._connect(request)
^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection.py”, line 124, in _connect
stream = self._network_backend.connect_tcp(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_backends/sync.py”, line 207, in connect_tcp
with map_exceptions(exc_map):
^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/contextlib.py”, line 158, in exit
self.gen.throw(value)
File “/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_exceptions.py”, line 14, in map_exceptions
raise to_exc(exc) from exc
httpcore.ConnectError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/exec_code.py”, line 88, in exec_func_with_error_handling
result = func()
^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 579, in code_to_exec
exec(code, module.dict)
File “/mount/src/streamlit/st_llama.py”, line 57, in
main()
File “/mount/src/streamlit/st_llama.py”, line 46, in main
result = ollama.chat(model=chat_model, messages=[{
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py”, line 332, in chat
return self._request(
^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py”, line 177, in _request
return cls(**self._request_raw(*args, **kwargs).json())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py”, line 118, in _request_raw
r = self._client.request(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py”, line 837, in request
return self.send(request, auth=auth, follow_redirects=follow_redirects)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py”, line 926, in send
response = self._send_handling_auth(
^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py”, line 954, in _send_handling_auth
response = self._send_handling_redirects(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py”, line 991, in _send_handling_redirects
response = self._send_single_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py”, line 1027, in _send_single_request
response = transport.handle_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.py”, line 235, in handle_request
with map_httpcore_exceptions():
^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/contextlib.py”, line 158, in exit
self.gen.throw(value)
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.py”, line 89, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.ConnectError: [Errno 111] Connection refused
────────────────────── Traceback (most recent call last) ───────────────────────
/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.
py:72 in map_httpcore_exceptions

 69 @contextlib.contextmanager                                              
 70 def map_httpcore_exceptions() -> typing.Iterator[None]:                 
 71 │   try:                                                                

❱ 72 │ │ yield
73 │ except Exception as exc:
74 │ │ mapped_exc = None
75

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.
py:236 in handle_request

233 │   │   │   extensions=request.extensions,                              
234 │   │   )                                                               
235 │   │   with map_httpcore_exceptions():                                 

❱ 236 │ │ │ resp = self._pool.handle_request(req)
237 │ │
238 │ │ assert isinstance(resp.stream, typing.Iterable)
239

/home/adminuser/venv/lib/python3.12/site-packages/httpcore/sync/connection
pool.py:256 in handle_request

253 │   │   │   │   closing = self._assign_requests_to_connections()        
254 │   │   │                                                               
255 │   │   │   self._close_connections(closing)                            

❱ 256 │ │ │ raise exc from None
257 │ │
258 │ │ # Return the response. Note that in this case we still have to
259 │ │ # the point at which the response is closed.

/home/adminuser/venv/lib/python3.12/site-packages/httpcore/sync/connection
pool.py:236 in handle_request

233 │   │   │   │                                                           
234 │   │   │   │   try:                                                    
235 │   │   │   │   │   # Send the request on the assigned connection.      

❱ 236 │ │ │ │ │ response = connection.handle_request(
237 │ │ │ │ │ │ pool_request.request
238 │ │ │ │ │ )
239 │ │ │ │ except ConnectionNotAvailable:

/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection.
py:101 in handle_request

 98 │   │   │   │   │   │   )                                               
 99 │   │   except BaseException as exc:                                    
100 │   │   │   self._connect_failed = True                                 

❱ 101 │ │ │ raise exc
102 │ │
103 │ │ return self._connection.handle_request(request)
104

/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection.
py:78 in handle_request

 75 │   │   try:                                                            
 76 │   │   │   with self._request_lock:                                    
 77 │   │   │   │   if self._connection is None:                            

❱ 78 │ │ │ │ │ stream = self._connect(request)
79 │ │ │ │ │
80 │ │ │ │ │ ssl_object = stream.get_extra_info(“ssl_object”)
81 │ │ │ │ │ http2_negotiated = (

/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection.
py:124 in _connect

121 │   │   │   │   │   │   "socket_options": self._socket_options,         
122 │   │   │   │   │   }                                                   
123 │   │   │   │   │   with Trace("connect_tcp", logger, request, kwargs)  

❱ 124 │ │ │ │ │ │ stream = self._network_backend.connect_tcp(**k
125 │ │ │ │ │ │ trace.return_value = stream
126 │ │ │ │ else:
127 │ │ │ │ │ kwargs = {

/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_backends/sync.py
:207 in connect_tcp

204 │   │   │   OSError: ConnectError,                                      
205 │   │   }                                                               
206 │   │                                                                   

❱ 207 │ │ with map_exceptions(exc_map):
208 │ │ │ sock = socket.create_connection(
209 │ │ │ │ address,
210 │ │ │ │ timeout,

/usr/local/lib/python3.12/contextlib.py:158 in exit

155 │   │   │   │   # tell if we get the same exception back                
156 │   │   │   │   value = typ()                                           
157 │   │   │   try:                                                        

❱ 158 │ │ │ │ self.gen.throw(value)
159 │ │ │ except StopIteration as exc:
160 │ │ │ │ # Suppress StopIteration unless it’s the same except
161 │ │ │ │ # was passed to throw(). This prevents a StopIteratio

/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_exceptions.py:14
in map_exceptions

11 │   except Exception as exc:  # noqa: PIE786                             
12 │   │   for from_exc, to_exc in map.items():                             
13 │   │   │   if isinstance(exc, from_exc):                                

❱ 14 │ │ │ │ raise to_exc(exc) from exc
15 │ │ raise # pragma: nocover
16
17
────────────────────────────────────────────────────────────────────────────────
ConnectError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

────────────────────── Traceback (most recent call last) ───────────────────────
/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptru
nner/exec_code.py:88 in exec_func_with_error_handling

/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptru
nner/script_runner.py:579 in code_to_exec

/mount/src/streamlit/st_llama.py:57 in

54 │   │   │   st.write_stream(stream_lines(response))                      
55                                                                          
56 if __name__ == "__main__":                                               

❱ 57 │ main()
58

/mount/src/streamlit/st_llama.py:46 in main

43 │   │   # st.write(ollama.list())                                        
44 │   │   chat_model = "llama3"                                            
45 │   │   with st.spinner("Thinking ..."):                                 

❱ 46 │ │ │ result = ollama.chat(model=chat_model, messages=[{
47 │ │ │ │ “role”: “user”,
48 │ │ │ │ “content”: prompt,
49 │ │ │ }])

/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py:332 in
chat

 329 │                                                                      
 330 │   Returns `ChatResponse` if `stream` is `False`, otherwise returns   
 331 │   """                                                                

❱ 332 │ return self._request(
333 │ ChatResponse,
334 │ ‘POST’,
335 │ ‘/api/chat’,

/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py:177 in
_request

 174 │                                                                      
 175 │     return inner()                                                   
 176 │                                                                      

❱ 177 │ return cls(**self._request_raw(*args, **kwargs).json())
178
179 @overload
180 def generate(

/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py:118 in
_request_raw

 115 │   super().__init__(httpx.Client, host, **kwargs)                     
 116                                                                        
 117   def _request_raw(self, *args, **kwargs):                             

❱ 118 │ r = self._client.request(*args, **kwargs)
119 │ try:
120 │ r.raise_for_status()
121 │ except httpx.HTTPStatusError as e:

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py:837 in
request

 834 │   │   │   timeout=timeout,                                           
 835 │   │   │   extensions=extensions,                                     
 836 │   │   )                                                              

❱ 837 │ │ return self.send(request, auth=auth, follow_redirects=follow_
838 │
839 │ @contextmanager
840 │ def stream(

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py:926 in
send

 923 │   │                                                                  
 924 │   │   auth = self._build_request_auth(request, auth)                 
 925 │   │                                                                  

❱ 926 │ │ response = self._send_handling_auth(
927 │ │ │ request,
928 │ │ │ auth=auth,
929 │ │ │ follow_redirects=follow_redirects,

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py:954 in
_send_handling_auth

 951 │   │   │   request = next(auth_flow)                                  
 952 │   │   │                                                              
 953 │   │   │   while True:                                                

❱ 954 │ │ │ │ response = self._send_handling_redirects(
955 │ │ │ │ │ request,
956 │ │ │ │ │ follow_redirects=follow_redirects,
957 │ │ │ │ │ history=history,

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py:991 in
_send_handling_redirects

 988 │   │   │   for hook in self._event_hooks["request"]:                  
 989 │   │   │   │   hook(request)                                          
 990 │   │   │                                                              

❱ 991 │ │ │ response = self._send_single_request(request)
992 │ │ │ try:
993 │ │ │ │ for hook in self._event_hooks[“response”]:
994 │ │ │ │ │ hook(response)

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py:1027 in
_send_single_request

1024 │   │   │   )                                                          
1025 │   │                                                                  
1026 │   │   with request_context(request=request):                         

❱ 1027 │ │ │ response = transport.handle_request(request)
1028 │ │
1029 │ │ assert isinstance(response.stream, SyncByteStream)
1030

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.
py:235 in handle_request

232 │   │   │   content=request.stream,                                     
233 │   │   │   extensions=request.extensions,                              
234 │   │   )                                                               

❱ 235 │ │ with map_httpcore_exceptions():
236 │ │ │ resp = self._pool.handle_request(req)
237 │ │
238 │ │ assert isinstance(resp.stream, typing.Iterable)

/usr/local/lib/python3.12/contextlib.py:158 in exit

155 │   │   │   │   # tell if we get the same exception back                
156 │   │   │   │   value = typ()                                           
157 │   │   │   try:                                                        

❱ 158 │ │ │ │ self.gen.throw(value)
159 │ │ │ except StopIteration as exc:
160 │ │ │ │ # Suppress StopIteration unless it’s the same except
161 │ │ │ │ # was passed to throw(). This prevents a StopIteratio

/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.
py:89 in map_httpcore_exceptions

 86 │   │   │   raise                                                       
 87 │   │                                                                   
 88 │   │   message = str(exc)                                              

❱ 89 │ │ raise mapped_exc(message) from exc
90
91
92 HTTPCORE_EXC_MAP = {
────────────────────────────────────────────────────────────────────────────────
ConnectError: [Errno 111] Connection refused
2025-01-04 04:58:45.899 Uncaught app execution
Traceback (most recent call last):
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.py”, line 72, in map_httpcore_exceptions
yield
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.py”, line 236, in handle_request
resp = self._pool.handle_request(req)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py”, line 256, in handle_request
raise exc from None
File “/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py”, line 236, in handle_request
response = connection.handle_request(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection.py”, line 101, in handle_request
raise exc
File “/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection.py”, line 78, in handle_request
stream = self._connect(request)
^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_sync/connection.py”, line 124, in _connect
stream = self._network_backend.connect_tcp(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_backends/sync.py”, line 207, in connect_tcp
with map_exceptions(exc_map):
^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/contextlib.py”, line 158, in exit
self.gen.throw(value)
File “/home/adminuser/venv/lib/python3.12/site-packages/httpcore/_exceptions.py”, line 14, in map_exceptions
raise to_exc(exc) from exc
httpcore.ConnectError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/exec_code.py”, line 88, in exec_func_with_error_handling
result = func()
^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 579, in code_to_exec
exec(code, module.dict)
File “/mount/src/streamlit/st_llama.py”, line 57, in
main()
File “/mount/src/streamlit/st_llama.py”, line 46, in main
result = ollama.chat(model=chat_model, messages=[{
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py”, line 332, in chat
return self._request(
^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py”, line 177, in _request
return cls(**self._request_raw(*args, **kwargs).json())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/ollama/_client.py”, line 118, in _request_raw
r = self._client.request(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py”, line 837, in request
return self.send(request, auth=auth, follow_redirects=follow_redirects)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py”, line 926, in send
response = self._send_handling_auth(
^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py”, line 954, in _send_handling_auth
response = self._send_handling_redirects(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py”, line 991, in _send_handling_redirects
response = self._send_single_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_client.py”, line 1027, in _send_single_request
response = transport.handle_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.py”, line 235, in handle_request
with map_httpcore_exceptions():
^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/contextlib.py”, line 158, in exit
self.gen.throw(value)
File “/home/adminuser/venv/lib/python3.12/site-packages/httpx/_transports/default.py”, line 89, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.ConnectError: [Errno 111] Connection refused

You can try call ollama via API like this. It works well for me when i use ollama on streamlitcloud

response = requests.post(
                            "http://localhost:11434/api/generate",
                            json={"modelfile": modelfile, "model": "llama3.2", "prompt": user_input,  "stream": False}
                        )

and if you deloyed it on streamcloud and have problem with access url local host, you can use ngrok to convert your local host http://localhost:11434 into public url
You can see it in this post Access Denied Using Ngrok