Upload app.py
Browse files
app.py
CHANGED
|
@@ -2,42 +2,36 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
import re
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
# Global variables for model and tokenizer
|
| 8 |
model = None
|
| 9 |
tokenizer = None
|
| 10 |
-
model_loaded = False
|
| 11 |
|
| 12 |
def load_model():
|
| 13 |
"""Load the model and tokenizer"""
|
| 14 |
-
global model, tokenizer
|
| 15 |
|
| 16 |
try:
|
| 17 |
print("Loading AEGIS Conduct Economic Analysis Model...")
|
| 18 |
|
| 19 |
-
# Load tokenizer and model from
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 21 |
-
"Gaston895/aegisconduct",
|
| 22 |
-
subfolder="econ",
|
| 23 |
trust_remote_code=True
|
| 24 |
)
|
| 25 |
|
| 26 |
model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
"Gaston895/aegisconduct",
|
| 28 |
-
subfolder="econ",
|
| 29 |
torch_dtype=torch.bfloat16,
|
| 30 |
device_map="auto",
|
| 31 |
trust_remote_code=True
|
| 32 |
)
|
| 33 |
|
| 34 |
print("Model loaded successfully!")
|
| 35 |
-
model_loaded = True
|
| 36 |
return True
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
print(f"Error loading model: {e}")
|
| 40 |
-
model_loaded = False
|
| 41 |
return False
|
| 42 |
|
| 43 |
def format_response(text):
|
|
@@ -53,10 +47,10 @@ def format_response(text):
|
|
| 53 |
|
| 54 |
def generate_response(message, history, temperature=0.7, max_tokens=512):
|
| 55 |
"""Generate response from the model"""
|
| 56 |
-
global model, tokenizer
|
| 57 |
|
| 58 |
-
if
|
| 59 |
-
return "Model
|
| 60 |
|
| 61 |
try:
|
| 62 |
# Build conversation context
|
|
@@ -115,10 +109,13 @@ def chat_interface(message, history, temperature, max_tokens):
|
|
| 115 |
|
| 116 |
return history, ""
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
# Create Gradio interface
|
| 119 |
with gr.Blocks(
|
| 120 |
-
title="AEGIS Conduct - Economic Analysis Chat"
|
| 121 |
-
theme=gr.themes.Soft()
|
| 122 |
) as demo:
|
| 123 |
|
| 124 |
gr.Markdown("""
|
|
@@ -132,6 +129,9 @@ with gr.Blocks(
|
|
| 132 |
Ask questions about economics, finance, market analysis, policy impacts, and more!
|
| 133 |
""")
|
| 134 |
|
|
|
|
|
|
|
|
|
|
| 135 |
with gr.Row():
|
| 136 |
with gr.Column(scale=4):
|
| 137 |
chatbot = gr.Chatbot(
|
|
@@ -143,12 +143,13 @@ with gr.Blocks(
|
|
| 143 |
msg = gr.Textbox(
|
| 144 |
placeholder="Ask me about economics, finance, markets, or any analytical question...",
|
| 145 |
show_label=False,
|
| 146 |
-
container=False
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
with gr.Row():
|
| 150 |
-
submit_btn = gr.Button("Send", variant="primary")
|
| 151 |
-
clear_btn = gr.Button("Clear Chat")
|
| 152 |
|
| 153 |
with gr.Column(scale=1):
|
| 154 |
gr.Markdown("### Settings")
|
|
@@ -158,7 +159,8 @@ with gr.Blocks(
|
|
| 158 |
maximum=2.0,
|
| 159 |
value=0.7,
|
| 160 |
step=0.1,
|
| 161 |
-
label="Temperature"
|
|
|
|
| 162 |
)
|
| 163 |
|
| 164 |
max_tokens = gr.Slider(
|
|
@@ -166,7 +168,8 @@ with gr.Blocks(
|
|
| 166 |
maximum=1024,
|
| 167 |
value=512,
|
| 168 |
step=50,
|
| 169 |
-
label="Max Response Length"
|
|
|
|
| 170 |
)
|
| 171 |
|
| 172 |
gr.Markdown("""
|
|
@@ -203,10 +206,11 @@ with gr.Blocks(
|
|
| 203 |
outputs=[chatbot, msg]
|
| 204 |
)
|
| 205 |
|
| 206 |
-
#
|
| 207 |
-
print("Initializing AEGIS Conduct Chat Interface...")
|
| 208 |
-
load_model()
|
| 209 |
-
|
| 210 |
-
# Launch the app
|
| 211 |
if __name__ == "__main__":
|
| 212 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
import re
|
|
|
|
| 5 |
|
| 6 |
# Global variables for model and tokenizer
|
| 7 |
model = None
|
| 8 |
tokenizer = None
|
|
|
|
| 9 |
|
| 10 |
def load_model():
|
| 11 |
"""Load the model and tokenizer"""
|
| 12 |
+
global model, tokenizer
|
| 13 |
|
| 14 |
try:
|
| 15 |
print("Loading AEGIS Conduct Economic Analysis Model...")
|
| 16 |
|
| 17 |
+
# Load tokenizer and model directly from root (no subfolder needed)
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 19 |
+
"Gaston895/aegisconduct",
|
|
|
|
| 20 |
trust_remote_code=True
|
| 21 |
)
|
| 22 |
|
| 23 |
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
"Gaston895/aegisconduct",
|
|
|
|
| 25 |
torch_dtype=torch.bfloat16,
|
| 26 |
device_map="auto",
|
| 27 |
trust_remote_code=True
|
| 28 |
)
|
| 29 |
|
| 30 |
print("Model loaded successfully!")
|
|
|
|
| 31 |
return True
|
| 32 |
|
| 33 |
except Exception as e:
|
| 34 |
print(f"Error loading model: {e}")
|
|
|
|
| 35 |
return False
|
| 36 |
|
| 37 |
def format_response(text):
|
|
|
|
| 47 |
|
| 48 |
def generate_response(message, history, temperature=0.7, max_tokens=512):
|
| 49 |
"""Generate response from the model"""
|
| 50 |
+
global model, tokenizer
|
| 51 |
|
| 52 |
+
if model is None or tokenizer is None:
|
| 53 |
+
return "Model not loaded. Please wait for initialization to complete."
|
| 54 |
|
| 55 |
try:
|
| 56 |
# Build conversation context
|
|
|
|
| 109 |
|
| 110 |
return history, ""
|
| 111 |
|
| 112 |
+
# Load model on startup
|
| 113 |
+
print("Initializing AEGIS Conduct Chat Interface...")
|
| 114 |
+
model_loaded = load_model()
|
| 115 |
+
|
| 116 |
# Create Gradio interface
|
| 117 |
with gr.Blocks(
|
| 118 |
+
title="AEGIS Conduct - Economic Analysis Chat"
|
|
|
|
| 119 |
) as demo:
|
| 120 |
|
| 121 |
gr.Markdown("""
|
|
|
|
| 129 |
Ask questions about economics, finance, market analysis, policy impacts, and more!
|
| 130 |
""")
|
| 131 |
|
| 132 |
+
if not model_loaded:
|
| 133 |
+
gr.Markdown("⚠️ **Model Loading Error**: Please refresh the page or contact support.")
|
| 134 |
+
|
| 135 |
with gr.Row():
|
| 136 |
with gr.Column(scale=4):
|
| 137 |
chatbot = gr.Chatbot(
|
|
|
|
| 143 |
msg = gr.Textbox(
|
| 144 |
placeholder="Ask me about economics, finance, markets, or any analytical question...",
|
| 145 |
show_label=False,
|
| 146 |
+
container=False,
|
| 147 |
+
scale=7
|
| 148 |
)
|
| 149 |
|
| 150 |
with gr.Row():
|
| 151 |
+
submit_btn = gr.Button("Send", variant="primary", scale=1)
|
| 152 |
+
clear_btn = gr.Button("Clear Chat", scale=1)
|
| 153 |
|
| 154 |
with gr.Column(scale=1):
|
| 155 |
gr.Markdown("### Settings")
|
|
|
|
| 159 |
maximum=2.0,
|
| 160 |
value=0.7,
|
| 161 |
step=0.1,
|
| 162 |
+
label="Temperature",
|
| 163 |
+
info="Controls randomness (0.1=focused, 2.0=creative)"
|
| 164 |
)
|
| 165 |
|
| 166 |
max_tokens = gr.Slider(
|
|
|
|
| 168 |
maximum=1024,
|
| 169 |
value=512,
|
| 170 |
step=50,
|
| 171 |
+
label="Max Response Length",
|
| 172 |
+
info="Maximum tokens in response"
|
| 173 |
)
|
| 174 |
|
| 175 |
gr.Markdown("""
|
|
|
|
| 206 |
outputs=[chatbot, msg]
|
| 207 |
)
|
| 208 |
|
| 209 |
+
# Launch configuration
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
if __name__ == "__main__":
|
| 211 |
+
demo.launch(
|
| 212 |
+
server_name="0.0.0.0",
|
| 213 |
+
server_port=7860,
|
| 214 |
+
share=False,
|
| 215 |
+
theme=gr.themes.Soft()
|
| 216 |
+
)
|