Gaston895 commited on
Commit
e32498e
·
verified ·
1 Parent(s): e408e4a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +229 -0
app.py ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
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 from the econ subdirectory
18
+ tokenizer = AutoTokenizer.from_pretrained(
19
+ "Gaston895/aegisconduct",
20
+ subfolder="econ",
21
+ trust_remote_code=True
22
+ )
23
+
24
+ model = AutoModelForCausalLM.from_pretrained(
25
+ "Gaston895/aegisconduct",
26
+ subfolder="econ",
27
+ torch_dtype=torch.bfloat16,
28
+ device_map="auto",
29
+ trust_remote_code=True
30
+ )
31
+
32
+ print("Model loaded successfully!")
33
+ return True
34
+
35
+ except Exception as e:
36
+ print(f"Error loading model: {e}")
37
+ return False
38
+
39
+ def format_response(text):
40
+ """Clean and format the model response"""
41
+ # Remove thinking tags if present
42
+ text = re.sub(r'<thinking>.*?</thinking>', '', text, flags=re.DOTALL)
43
+
44
+ # Clean up extra whitespace
45
+ text = re.sub(r'\n\s*\n', '\n\n', text)
46
+ text = text.strip()
47
+
48
+ return text
49
+
50
+ def generate_response(message, history, temperature=0.7, max_tokens=512):
51
+ """Generate response from the model"""
52
+ global model, tokenizer
53
+
54
+ if model is None or tokenizer is None:
55
+ return "Model not loaded. Please wait for initialization to complete."
56
+
57
+ try:
58
+ # Build conversation context
59
+ conversation = ""
60
+ for user_msg, assistant_msg in history:
61
+ conversation += f"User: {user_msg}\nAssistant: {assistant_msg}\n\n"
62
+
63
+ # Add current message
64
+ conversation += f"User: {message}\nAssistant:"
65
+
66
+ # Tokenize input
67
+ inputs = tokenizer(conversation, return_tensors="pt", truncation=True, max_length=2048)
68
+
69
+ # Move to device
70
+ if torch.cuda.is_available():
71
+ inputs = {k: v.to(model.device) for k, v in inputs.items()}
72
+
73
+ # Generate response
74
+ with torch.no_grad():
75
+ outputs = model.generate(
76
+ **inputs,
77
+ max_new_tokens=max_tokens,
78
+ temperature=temperature,
79
+ do_sample=True,
80
+ top_p=0.95,
81
+ top_k=40,
82
+ repetition_penalty=1.05,
83
+ pad_token_id=tokenizer.eos_token_id,
84
+ eos_token_id=tokenizer.eos_token_id
85
+ )
86
+
87
+ # Decode response
88
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
89
+
90
+ # Extract only the new response
91
+ response = response[len(conversation):].strip()
92
+
93
+ # Format and clean response
94
+ response = format_response(response)
95
+
96
+ return response
97
+
98
+ except Exception as e:
99
+ return f"Error generating response: {str(e)}"
100
+
101
+ def chat_interface(message, history, temperature, max_tokens):
102
+ """Main chat interface function"""
103
+ if not message.strip():
104
+ return history, ""
105
+
106
+ # Generate response
107
+ response = generate_response(message, history, temperature, max_tokens)
108
+
109
+ # Add to history
110
+ history.append((message, response))
111
+
112
+ return history, ""
113
+
114
+ # Load model on startup
115
+ print("Initializing AEGIS Conduct Chat Interface...")
116
+ model_loaded = load_model()
117
+
118
+ # Create Gradio interface
119
+ with gr.Blocks(
120
+ title="AEGIS Conduct - Economic Analysis Chat",
121
+ theme=gr.themes.Soft(),
122
+ css="""
123
+ .gradio-container {
124
+ max-width: 1000px !important;
125
+ }
126
+ .chat-message {
127
+ padding: 10px;
128
+ margin: 5px 0;
129
+ border-radius: 10px;
130
+ }
131
+ """
132
+ ) as demo:
133
+
134
+ gr.Markdown("""
135
+ # 🤖 AEGIS Conduct - Economic Analysis Chat
136
+
137
+ Chat with an AI model specialized in economic and financial analysis. This model features:
138
+ - **Thinking Mode**: Automatic activation for complex reasoning
139
+ - **Economic Expertise**: Specialized knowledge in finance, markets, and policy
140
+ - **128k Context**: Extended memory for detailed conversations
141
+
142
+ Ask questions about economics, finance, market analysis, policy impacts, and more!
143
+ """)
144
+
145
+ if not model_loaded:
146
+ gr.Markdown("⚠️ **Model Loading Error**: Please refresh the page or contact support.")
147
+
148
+ with gr.Row():
149
+ with gr.Column(scale=4):
150
+ chatbot = gr.Chatbot(
151
+ height=500,
152
+ show_label=False,
153
+ container=True,
154
+ bubble_full_width=False
155
+ )
156
+
157
+ msg = gr.Textbox(
158
+ placeholder="Ask me about economics, finance, markets, or any analytical question...",
159
+ show_label=False,
160
+ container=False,
161
+ scale=7
162
+ )
163
+
164
+ with gr.Row():
165
+ submit_btn = gr.Button("Send", variant="primary", scale=1)
166
+ clear_btn = gr.Button("Clear Chat", scale=1)
167
+
168
+ with gr.Column(scale=1):
169
+ gr.Markdown("### Settings")
170
+
171
+ temperature = gr.Slider(
172
+ minimum=0.1,
173
+ maximum=2.0,
174
+ value=0.7,
175
+ step=0.1,
176
+ label="Temperature",
177
+ info="Controls randomness (0.1=focused, 2.0=creative)"
178
+ )
179
+
180
+ max_tokens = gr.Slider(
181
+ minimum=50,
182
+ maximum=1024,
183
+ value=512,
184
+ step=50,
185
+ label="Max Response Length",
186
+ info="Maximum tokens in response"
187
+ )
188
+
189
+ gr.Markdown("""
190
+ ### Example Questions
191
+ - Analyze the impact of inflation on consumer spending
192
+ - Explain quantitative easing and its effects
193
+ - What are the risks of high national debt?
194
+ - How do interest rates affect the stock market?
195
+ - Think deeply: What causes economic recessions?
196
+ """)
197
+
198
+ # Event handlers
199
+ def submit_message(message, history, temp, max_tok):
200
+ return chat_interface(message, history, temp, max_tok)
201
+
202
+ def clear_chat():
203
+ return [], ""
204
+
205
+ # Bind events
206
+ submit_btn.click(
207
+ submit_message,
208
+ inputs=[msg, chatbot, temperature, max_tokens],
209
+ outputs=[chatbot, msg]
210
+ )
211
+
212
+ msg.submit(
213
+ submit_message,
214
+ inputs=[msg, chatbot, temperature, max_tokens],
215
+ outputs=[chatbot, msg]
216
+ )
217
+
218
+ clear_btn.click(
219
+ clear_chat,
220
+ outputs=[chatbot, msg]
221
+ )
222
+
223
+ # Launch configuration
224
+ if __name__ == "__main__":
225
+ demo.launch(
226
+ server_name="0.0.0.0",
227
+ server_port=7860,
228
+ share=False
229
+ )