File size: 2,811 Bytes
90ddb86 |
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 |
import streamlit as st
import torch
import numpy as np
import pandas as pd
from PIL import Image
import nltk
from transformers import AutoModelForCausalLM, AutoTokenizer
import yaml
# Download NLTK data
nltk.download('punkt')
# Load YAML configuration
@st.cache
def load_yaml(file_path):
with open(file_path, "r") as file:
return yaml.safe_load(file)
config = load_yaml("self-evolving-agent-prompt-en.yaml.txt")
# Load model and tokenizer
@st.cache(allow_output_mutation=True)
def load_model_and_tokenizer(model_path):
tokenizer = AutoTokenizer.from_pretrained("gpt2")
model = AutoModelForCausalLM.from_pretrained(
"gpt2",
state_dict=torch.load(model_path, map_location=torch.device("cpu")),
)
return tokenizer, model
tokenizer, model = load_model_and_tokenizer("flux_lustly-ai_v1.safetensors")
# Streamlit UI setup
st.set_page_config(page_title="NOVA Assistant", layout="wide")
st.title("NOVA Assistant")
st.markdown(config.get("description", "An advanced AI assistant."))
# User input
user_input = st.text_input("Enter your question or prompt:")
if user_input:
with st.spinner("Processing..."):
# Use NLTK to preprocess the input
sentences = nltk.sent_tokenize(user_input)
word_count = sum(len(nltk.word_tokenize(sentence)) for sentence in sentences)
# Display preprocessing stats
st.write(f"Preprocessing stats: {len(sentences)} sentences, {word_count} words")
# Generate AI response
prompt_template = config.get("prompt_template", "{input}")
prompt = prompt_template.replace("{input}", user_input)
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(inputs.input_ids, max_length=150, num_return_sequences=1)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Example: Use numpy for a dummy operation (e.g., scaling output length)
response_length = len(response.split())
scaled_length = np.sqrt(response_length) # Example use of numpy
st.write(f"Response length (scaled): {scaled_length:.2f}")
st.subheader("AI Response:")
st.write(response)
# Adding a sample DataFrame with Pandas
st.sidebar.header("Sample Data")
data = {
"Input Length": [5, 10, 20],
"Response Length": [15, 25, 35],
"AI Confidence": [0.8, 0.9, 0.95]
}
df = pd.DataFrame(data)
st.sidebar.write("Sample DataFrame:")
st.sidebar.dataframe(df)
# Image processing with Pillow (optional)
uploaded_file = st.file_uploader("Upload an image (optional):", type=["png", "jpg", "jpeg"])
if uploaded_file:
img = Image.open(uploaded_file)
st.image(img, caption="Uploaded Image", use_column_width=True)
st.write(f"Image Size: {img.size} (Width x Height)")
|