Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import nltk
|
| 7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 8 |
+
import yaml
|
| 9 |
+
|
| 10 |
+
# Download NLTK data
|
| 11 |
+
nltk.download('punkt')
|
| 12 |
+
|
| 13 |
+
# Load YAML configuration
|
| 14 |
+
@st.cache
|
| 15 |
+
def load_yaml(file_path):
|
| 16 |
+
with open(file_path, "r") as file:
|
| 17 |
+
return yaml.safe_load(file)
|
| 18 |
+
|
| 19 |
+
config = load_yaml("self-evolving-agent-prompt-en.yaml.txt")
|
| 20 |
+
|
| 21 |
+
# Load model and tokenizer
|
| 22 |
+
@st.cache(allow_output_mutation=True)
|
| 23 |
+
def load_model_and_tokenizer(model_path):
|
| 24 |
+
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 25 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 26 |
+
"gpt2",
|
| 27 |
+
state_dict=torch.load(model_path, map_location=torch.device("cpu")),
|
| 28 |
+
)
|
| 29 |
+
return tokenizer, model
|
| 30 |
+
|
| 31 |
+
tokenizer, model = load_model_and_tokenizer("flux_lustly-ai_v1.safetensors")
|
| 32 |
+
|
| 33 |
+
# Streamlit UI setup
|
| 34 |
+
st.set_page_config(page_title="NOVA Assistant", layout="wide")
|
| 35 |
+
st.title("NOVA Assistant")
|
| 36 |
+
st.markdown(config.get("description", "An advanced AI assistant."))
|
| 37 |
+
|
| 38 |
+
# User input
|
| 39 |
+
user_input = st.text_input("Enter your question or prompt:")
|
| 40 |
+
|
| 41 |
+
if user_input:
|
| 42 |
+
with st.spinner("Processing..."):
|
| 43 |
+
# Use NLTK to preprocess the input
|
| 44 |
+
sentences = nltk.sent_tokenize(user_input)
|
| 45 |
+
word_count = sum(len(nltk.word_tokenize(sentence)) for sentence in sentences)
|
| 46 |
+
|
| 47 |
+
# Display preprocessing stats
|
| 48 |
+
st.write(f"Preprocessing stats: {len(sentences)} sentences, {word_count} words")
|
| 49 |
+
|
| 50 |
+
# Generate AI response
|
| 51 |
+
prompt_template = config.get("prompt_template", "{input}")
|
| 52 |
+
prompt = prompt_template.replace("{input}", user_input)
|
| 53 |
+
|
| 54 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 55 |
+
outputs = model.generate(inputs.input_ids, max_length=150, num_return_sequences=1)
|
| 56 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 57 |
+
|
| 58 |
+
# Example: Use numpy for a dummy operation (e.g., scaling output length)
|
| 59 |
+
response_length = len(response.split())
|
| 60 |
+
scaled_length = np.sqrt(response_length) # Example use of numpy
|
| 61 |
+
st.write(f"Response length (scaled): {scaled_length:.2f}")
|
| 62 |
+
|
| 63 |
+
st.subheader("AI Response:")
|
| 64 |
+
st.write(response)
|
| 65 |
+
|
| 66 |
+
# Adding a sample DataFrame with Pandas
|
| 67 |
+
st.sidebar.header("Sample Data")
|
| 68 |
+
data = {
|
| 69 |
+
"Input Length": [5, 10, 20],
|
| 70 |
+
"Response Length": [15, 25, 35],
|
| 71 |
+
"AI Confidence": [0.8, 0.9, 0.95]
|
| 72 |
+
}
|
| 73 |
+
df = pd.DataFrame(data)
|
| 74 |
+
st.sidebar.write("Sample DataFrame:")
|
| 75 |
+
st.sidebar.dataframe(df)
|
| 76 |
+
|
| 77 |
+
# Image processing with Pillow (optional)
|
| 78 |
+
uploaded_file = st.file_uploader("Upload an image (optional):", type=["png", "jpg", "jpeg"])
|
| 79 |
+
if uploaded_file:
|
| 80 |
+
img = Image.open(uploaded_file)
|
| 81 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 82 |
+
st.write(f"Image Size: {img.size} (Width x Height)")
|
| 83 |
+
|