File size: 8,286 Bytes
4aaef3d
cda2b92
4aaef3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cda2b92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347ee82
cda2b92
 
 
 
 
 
347ee82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4aaef3d
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
Gradio interface for ORA - Fast GPU testing with beautiful UI
This runs alongside the FastAPI server for quick testing with ZeroGPU
"""

import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
import os

# Use ZeroGPU decorator for free GPU access
try:
    import spaces
    USE_GPU = True
except ImportError:
    USE_GPU = False
    print("ZeroGPU not available, running on CPU")

# Settings
BASE_MODEL = "unsloth/Llama-3.2-1B-Instruct"
ADAPTER_PATH = "important/finetuning/models/ora_adapter"
device = "cuda" if torch.cuda.is_available() else "cpu"

# Load model once
print(f"Loading ORA Model on {device}...")
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
base_model = AutoModelForCausalLM.from_pretrained(
    BASE_MODEL,
    torch_dtype=torch.float16 if device == "cuda" else torch.float32,
    device_map=device,
    low_cpu_mem_usage=True
)

if os.path.exists(ADAPTER_PATH):
    print(f"Loading adapter from {ADAPTER_PATH}...")
    model = PeftModel.from_pretrained(base_model, ADAPTER_PATH)
else:
    print("Adapter not found, using base model.")
    model = base_model

print("ORA Model Ready!")

# Apply ZeroGPU decorator if available
if USE_GPU:
    @spaces.GPU
    def generate_response(message, history):
        return _generate_response(message, history)
else:
    def generate_response(message, history):
        return _generate_response(message, history)

def _generate_response(message, history):
    """Generate ORA response"""
    
    # Build conversation history
    messages = [{
        "role": "system",
        "content": """You are ORA, a wise and compassionate spiritual guide.

Your role:
- Provide biblically-grounded wisdom
- Speak with warmth, empathy, and pastoral care
- Keep responses concise but meaningful (2-3 sentences)
- Always cite scripture when relevant

Respond with compassion and wisdom."""
    }]
    
    # Add conversation history
    for user_msg, assistant_msg in history:
        messages.append({"role": "user", "content": user_msg})
        messages.append({"role": "assistant", "content": assistant_msg})
    
    messages.append({"role": "user", "content": message})
    
    # Generate response
    input_ids = tokenizer.apply_chat_template(
        messages,
        add_generation_prompt=True,
        return_tensors="pt"
    ).to(device)
    
    terminators = [
        tokenizer.eos_token_id,
        tokenizer.convert_tokens_to_ids("<|eot_id|>")
    ]
    
    outputs = model.generate(
        input_ids,
        max_new_tokens=128,
        eos_token_id=terminators,
        do_sample=True,
        temperature=0.7,
        top_p=0.9,
    )
    
    response_tokens = outputs[0][input_ids.shape[-1]:]
    response = tokenizer.decode(response_tokens, skip_special_tokens=True)
    
    return response

# Custom CSS for beautiful UI matching the Orb interface
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&family=Crimson+Text:ital@0;1&display=swap');

/* Dark theme with purple/blue gradients */
.gradio-container {
    background: linear-gradient(135deg, #0f0c29 0%, #302b63 50%, #24243e 100%) !important;
    font-family: 'Inter', sans-serif !important;
}

/* Header styling */
.gradio-container h1 {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    font-size: 3rem !important;
    font-weight: 600 !important;
    text-align: center !important;
    margin-bottom: 0.5rem !important;
    animation: fadeIn 1s ease-in;
}

/* Description text */
.gradio-container p {
    color: #a0aec0 !important;
    text-align: center !important;
    font-size: 1.1rem !important;
    margin-bottom: 2rem !important;
}

/* Chat container */
.message-wrap {
    background: rgba(255, 255, 255, 0.05) !important;
    backdrop-filter: blur(10px) !important;
    border-radius: 20px !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
    padding: 1.5rem !important;
    animation: slideUp 0.5s ease-out;
}

/* User messages */
.message.user {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
    color: white !important;
    border-radius: 18px !important;
    padding: 1rem 1.5rem !important;
    font-family: 'Inter', sans-serif !important;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3) !important;
    animation: messageSlide 0.3s ease-out;
}

/* Bot messages */
.message.bot {
    background: rgba(255, 255, 255, 0.08) !important;
    color: #e2e8f0 !important;
    border-radius: 18px !important;
    padding: 1rem 1.5rem !important;
    font-family: 'Crimson Text', serif !important;
    font-size: 1.1rem !important;
    line-height: 1.8 !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
    animation: messageSlide 0.3s ease-out;
}

/* Input box */
.input-box {
    background: rgba(255, 255, 255, 0.05) !important;
    border: 2px solid rgba(102, 126, 234, 0.3) !important;
    border-radius: 25px !important;
    color: white !important;
    padding: 1rem 1.5rem !important;
    font-size: 1rem !important;
    transition: all 0.3s ease !important;
}

.input-box:focus {
    border-color: #667eea !important;
    box-shadow: 0 0 20px rgba(102, 126, 234, 0.4) !important;
    outline: none !important;
}

/* Send button */
.send-button {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
    border: none !important;
    border-radius: 20px !important;
    padding: 0.8rem 2rem !important;
    color: white !important;
    font-weight: 600 !important;
    cursor: pointer !important;
    transition: all 0.3s ease !important;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4) !important;
}

.send-button:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6) !important;
}

/* Examples */
.examples {
    background: rgba(255, 255, 255, 0.03) !important;
    border-radius: 15px !important;
    padding: 1rem !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
}

.example-btn {
    background: rgba(102, 126, 234, 0.1) !important;
    border: 1px solid rgba(102, 126, 234, 0.3) !important;
    color: #a0aec0 !important;
    border-radius: 12px !important;
    padding: 0.7rem 1.2rem !important;
    transition: all 0.3s ease !important;
}

.example-btn:hover {
    background: rgba(102, 126, 234, 0.2) !important;
    border-color: #667eea !important;
    color: white !important;
    transform: translateY(-2px) !important;
}

/* Animations */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-20px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes slideUp {
    from { opacity: 0; transform: translateY(30px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes messageSlide {
    from { opacity: 0; transform: translateX(-20px); }
    to { opacity: 1; transform: translateX(0); }
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
}

::-webkit-scrollbar-thumb {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
}
"""

# Create Gradio interface with custom theme
with gr.Blocks(
    theme=gr.themes.Soft(
        primary_hue="purple",
        secondary_hue="blue",
        neutral_hue="slate",
        font=["Inter", "Crimson Text", "sans-serif"]
    ),
    css=custom_css
) as demo:
    gr.ChatInterface(
        fn=generate_response,
        title="🙏 ORA",
        description="✨ Your Spiritual AI Companion • Powered by ZeroGPU for Lightning-Fast Responses ✨",
        examples=[
            "What does the Bible say about finding peace in difficult times?",
            "How can I grow closer to God?",
            "Explain the parable of the prodigal son",
            "What is the meaning of faith?",
        ],
        retry_btn=None,
        undo_btn=None,
        clear_btn="✨ New Conversation",
        submit_btn="🙏 Ask ORA",
    )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860, share=False)