File size: 1,863 Bytes
ddf840b
 
 
b009734
 
 
 
 
 
 
 
 
 
ddf840b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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}")