Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,9 @@ import pandas as pd
|
|
| 5 |
import numpy as np
|
| 6 |
from datetime import datetime, timedelta
|
| 7 |
from typing import Dict, List, Any
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# --- Data Processing Class ---
|
| 10 |
class DataProcessor:
|
|
@@ -356,27 +359,95 @@ def render_brainstorm_page():
|
|
| 356 |
else:
|
| 357 |
st.info("No products yet. Create one to get started!")
|
| 358 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
def render_chat():
|
| 360 |
-
st.header("💬 Business Assistant")
|
| 361 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
if "messages" not in st.session_state:
|
| 363 |
st.session_state.messages = []
|
| 364 |
|
|
|
|
| 365 |
for message in st.session_state.messages:
|
| 366 |
with st.chat_message(message["role"]):
|
| 367 |
st.markdown(message["content"])
|
| 368 |
|
|
|
|
| 369 |
if prompt := st.chat_input("Ask about your business..."):
|
|
|
|
| 370 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
|
|
|
|
| 371 |
with st.chat_message("user"):
|
| 372 |
st.markdown(prompt)
|
| 373 |
|
| 374 |
-
|
| 375 |
-
|
| 376 |
with st.chat_message("assistant"):
|
| 377 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 379 |
|
|
|
|
| 380 |
def main():
|
| 381 |
st.set_page_config(
|
| 382 |
page_title="Prospira",
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
from datetime import datetime, timedelta
|
| 7 |
from typing import Dict, List, Any
|
| 8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 9 |
+
import torch
|
| 10 |
+
import streamlit as st
|
| 11 |
|
| 12 |
# --- Data Processing Class ---
|
| 13 |
class DataProcessor:
|
|
|
|
| 359 |
else:
|
| 360 |
st.info("No products yet. Create one to get started!")
|
| 361 |
|
| 362 |
+
|
| 363 |
+
class LLaMAAssistant:
|
| 364 |
+
def __init__(self, model_name="meta-llama/Llama-2-7b-chat-hf"):
|
| 365 |
+
try:
|
| 366 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 367 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 368 |
+
model_name,
|
| 369 |
+
torch_dtype=torch.float16,
|
| 370 |
+
device_map="auto"
|
| 371 |
+
)
|
| 372 |
+
except Exception as e:
|
| 373 |
+
st.error(f"Model loading error: {e}")
|
| 374 |
+
self.model = None
|
| 375 |
+
self.tokenizer = None
|
| 376 |
+
|
| 377 |
+
def generate_response(self, prompt: str, context: list = None) -> str:
|
| 378 |
+
if not self.model or not self.tokenizer:
|
| 379 |
+
return "LLM not initialized. Please check model configuration."
|
| 380 |
+
|
| 381 |
+
# Prepare conversation context
|
| 382 |
+
if context is None:
|
| 383 |
+
context = []
|
| 384 |
+
|
| 385 |
+
# Create full prompt with conversation history
|
| 386 |
+
full_prompt = "".join([f"{msg['role']}: {msg['content']}\n" for msg in context])
|
| 387 |
+
full_prompt += f"user: {prompt}\nassistant: "
|
| 388 |
+
|
| 389 |
+
# Tokenize input
|
| 390 |
+
input_ids = self.tokenizer(full_prompt, return_tensors="pt").input_ids.to(self.model.device)
|
| 391 |
+
|
| 392 |
+
# Generate response
|
| 393 |
+
try:
|
| 394 |
+
output = self.model.generate(
|
| 395 |
+
input_ids,
|
| 396 |
+
max_length=500,
|
| 397 |
+
num_return_sequences=1,
|
| 398 |
+
no_repeat_ngram_size=2,
|
| 399 |
+
temperature=0.7,
|
| 400 |
+
top_p=0.9
|
| 401 |
+
)
|
| 402 |
+
|
| 403 |
+
# Decode response
|
| 404 |
+
response = self.tokenizer.decode(output[0], skip_special_tokens=True)
|
| 405 |
+
|
| 406 |
+
# Extract only the new part of the response
|
| 407 |
+
response = response[len(full_prompt):].strip()
|
| 408 |
+
|
| 409 |
+
return response
|
| 410 |
+
except Exception as e:
|
| 411 |
+
return f"Response generation error: {e}"
|
| 412 |
+
|
| 413 |
def render_chat():
|
| 414 |
+
st.header("💬 Business AI Assistant")
|
| 415 |
|
| 416 |
+
# Initialize LLaMA model (only once)
|
| 417 |
+
if 'llama_assistant' not in st.session_state:
|
| 418 |
+
st.session_state.llama_assistant = LLaMAAssistant()
|
| 419 |
+
|
| 420 |
+
# Initialize message history
|
| 421 |
if "messages" not in st.session_state:
|
| 422 |
st.session_state.messages = []
|
| 423 |
|
| 424 |
+
# Display chat history
|
| 425 |
for message in st.session_state.messages:
|
| 426 |
with st.chat_message(message["role"]):
|
| 427 |
st.markdown(message["content"])
|
| 428 |
|
| 429 |
+
# User input
|
| 430 |
if prompt := st.chat_input("Ask about your business..."):
|
| 431 |
+
# Add user message to chat history
|
| 432 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 433 |
+
|
| 434 |
+
# Display user message
|
| 435 |
with st.chat_message("user"):
|
| 436 |
st.markdown(prompt)
|
| 437 |
|
| 438 |
+
# Generate AI response
|
|
|
|
| 439 |
with st.chat_message("assistant"):
|
| 440 |
+
with st.spinner("Generating response..."):
|
| 441 |
+
response = st.session_state.llama_assistant.generate_response(
|
| 442 |
+
prompt,
|
| 443 |
+
st.session_state.messages[:-1] # Exclude current message
|
| 444 |
+
)
|
| 445 |
+
st.markdown(response)
|
| 446 |
+
|
| 447 |
+
# Add assistant response to history
|
| 448 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 449 |
|
| 450 |
+
# Replace the existing render_chat() function in your main.py with this implementation
|
| 451 |
def main():
|
| 452 |
st.set_page_config(
|
| 453 |
page_title="Prospira",
|