-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
146 lines (116 loc) · 5.06 KB
/
Copy pathapp.py
File metadata and controls
146 lines (116 loc) · 5.06 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import streamlit as st
import pandas as pd
import json
import tempfile
import os
from utils.file_handlers import handle_file_upload
from utils.model_utils import analyze_with_llama, get_available_models, pull_model
# App configuration
st.set_page_config(page_title="Local LLM Analyst", layout="wide")
def main():
st.title("🧠 Local LLM Analyst")
st.caption("Multimodal data analysis using local LLMs via Ollama")
# Check required models
required_models = ["llama3", "llava"]
available_models = get_available_models()
missing_models = [m for m in required_models if m not in available_models]
with st.sidebar:
st.header("🧠 Models Status")
# Check which models are already available
available_models = get_available_models()
required_models = ["llama3", "llava"]
missing_models = [m for m in required_models if m not in available_models]
if missing_models:
st.warning(f"⚠️ Missing model(s): {', '.join(missing_models)}")
for model in missing_models:
if st.button(f"📥 Download {model}"):
with st.spinner(f"Downloading {model}..."):
pull_model(model)
st.success(f"✅ {model} downloaded successfully.")
if hasattr(st, "experimental_rerun"):
st.experimental_rerun()
elif hasattr(st, "rerun"):
st.rerun()
else:
st.info("Model downloaded. Please refresh manually.")
else:
st.success("✅ All required models (llama3 & llava) are installed and ready to use!")
# Slider for creativity
temperature = st.slider(
"Creativity (temperature)",
min_value=0.0,
max_value=1.0,
value=0.7,
step=0.1
)
tab1, tab2 = st.tabs(["📄 Data Analysis", "🖼️ Image Analysis"])
# -------- TEXT / DATA TAB -------- #
with tab1:
st.subheader("Upload CSV, JSON, or TXT")
uploaded_file = st.file_uploader(
"Upload a file",
type=["csv", "json", "txt"]
)
if uploaded_file:
file_content = None
st.subheader("📊 File Preview")
if uploaded_file.type == "text/csv":
df = pd.read_csv(uploaded_file)
st.dataframe(df)
file_content = df.to_string()
elif uploaded_file.type == "application/json":
data = json.load(uploaded_file)
st.json(data)
file_content = json.dumps(data, indent=2)
else: # Plain text
file_content = uploaded_file.getvalue().decode("utf-8")
st.text(file_content[:1000] + "..." if len(file_content) > 1000 else file_content)
st.caption("💡 Exemples : 'Summarize this data', 'Find average age by city', 'Any outliers?'")
user_query = st.text_area(
"What would you like to analyze?",
placeholder="E.g., What are the key insights in this dataset?"
)
if st.button("Analyze") and user_query and file_content:
with st.spinner("Analyzing with llama3..."):
response = analyze_with_llama(
model="llama3",
prompt=user_query,
context=file_content,
temperature=temperature
)
st.subheader("🧠 Analysis Results")
st.write(response)
# -------- IMAGE TAB -------- #
with tab2:
st.subheader("Upload an image for analysis")
st.info("🧠 Requires a multimodal model like `llava`")
uploaded_image = st.file_uploader(
"Upload an image",
type=["jpg", "jpeg", "png"]
)
if uploaded_image:
if uploaded_image:
st.image(uploaded_image, caption="🖼️ Preview", use_container_width=True)
image_query = st.text_area(
"What do you want to know about this image?",
placeholder="E.g., What is shown in this image?"
)
if st.button("Analyze Image") and image_query:
with st.spinner("Analyzing with llava..."):
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
tmp.write(uploaded_image.getbuffer())
tmp_path = tmp.name
response = analyze_with_llama(
model="llava",
prompt=image_query,
image_path=tmp_path,
temperature=temperature
)
st.subheader("📌 Image Analysis Results")
st.write(response)
try:
os.remove(tmp_path)
except Exception:
pass
if __name__ == "__main__":
main()