Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import requests
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
# Titolo dell'app
|
6 |
+
st.title("💬 Chat con Ollama")
|
7 |
+
|
8 |
+
# Campo di input per l'utente
|
9 |
+
prompt = st.text_input("Inserisci il tuo prompt:", "")
|
10 |
+
|
11 |
+
# Bottone per inviare la richiesta
|
12 |
+
if st.button("Invia"):
|
13 |
+
if prompt.strip() == "":
|
14 |
+
st.warning("Inserisci un prompt valido!")
|
15 |
+
else:
|
16 |
+
# Mostra un indicatore di caricamento
|
17 |
+
with st.spinner("Generando risposta..."):
|
18 |
+
response = requests.post(
|
19 |
+
"http://localhost:11434/api/generate",
|
20 |
+
json={"model": "hf.co/hugging-quants/Llama-3.2-1B-Instruct-Q8_0-GGUF", "prompt": prompt},
|
21 |
+
stream=True
|
22 |
+
)
|
23 |
+
|
24 |
+
if response.status_code == 200:
|
25 |
+
full_response = ""
|
26 |
+
|
27 |
+
# Legge la risposta progressivamente
|
28 |
+
for line in response.iter_lines():
|
29 |
+
if line:
|
30 |
+
try:
|
31 |
+
json_data = line.decode("utf-8")
|
32 |
+
data = json.loads(json_data)
|
33 |
+
full_response += data.get("response", "")
|
34 |
+
|
35 |
+
|
36 |
+
if data.get("done", False):
|
37 |
+
st.write(full_response)
|
38 |
+
break
|
39 |
+
except json.JSONDecodeError:
|
40 |
+
st.error(f"Errore nel parsing JSON: {line}")
|
41 |
+
else:
|
42 |
+
st.error(f"Errore nella richiesta: {response.status_code}")
|