llamabot / app.py
vincepenta's picture
Update app.py
b009734 verified
import json
import requests
import streamlit as st
import subprocess
try:
result = subprocess.run(["ollama", "--version"], capture_output=True, text=True, check=True)
print("✅ Ollama è installato:", result.stdout.strip())
except FileNotFoundError:
print("❌ Ollama non è installato.")
except subprocess.CalledProcessError as e:
print("⚠️ Errore durante il controllo di Ollama:", e)
# Titolo dell'app
st.title("💬 Chat con Ollama")
# Campo di input per l'utente
prompt = st.text_input("Inserisci il tuo prompt:", "")
# Bottone per inviare la richiesta
if st.button("Invia"):
if prompt.strip() == "":
st.warning("Inserisci un prompt valido!")
else:
# Mostra un indicatore di caricamento
with st.spinner("Generando risposta..."):
response = requests.post(
"http://localhost:11434/api/generate",
json={"model": "hf.co/hugging-quants/Llama-3.2-1B-Instruct-Q8_0-GGUF", "prompt": prompt},
stream=True
)
if response.status_code == 200:
full_response = ""
# Legge la risposta progressivamente
for line in response.iter_lines():
if line:
try:
json_data = line.decode("utf-8")
data = json.loads(json_data)
full_response += data.get("response", "")
if data.get("done", False):
st.write(full_response)
break
except json.JSONDecodeError:
st.error(f"Errore nel parsing JSON: {line}")
else:
st.error(f"Errore nella richiesta: {response.status_code}")