File size: 8,636 Bytes
c7eab17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from unsloth import FastLanguageModel
import time

# Global variables for model
model = None
tokenizer = None

def load_model():
    """Load the Ideon Audio support model"""
    global model, tokenizer
    
    if model is None:
        print("πŸ”„ Loading Ideon Audio Support Model...")
        
        try:
            model, tokenizer = FastLanguageModel.from_pretrained(
                model_name="ainovatronsec/ideoaudio",
                max_seq_length=2048,
                dtype=None,
                load_in_4bit=True,
                trust_remote_code=False,
            )
            
            # Enable fast inference
            FastLanguageModel.for_inference(model)
            print("βœ… Model loaded successfully!")
            
        except Exception as e:
            print(f"❌ Error loading model: {e}")
            return False
    
    return True

def get_ideon_response(message, history, temperature=0.7, max_tokens=256):
    """Generate response from Ideon Audio support model"""
    
    # Load model if not already loaded
    if not load_model():
        return "❌ Sorry, the model failed to load. Please try again later."
    
    try:
        # Format message for the model
        messages = [{"role": "user", "content": message}]
        
        # Apply chat template
        inputs = tokenizer.apply_chat_template(
            messages,
            tokenize=True,
            add_generation_prompt=True,
            return_tensors="pt"
        )
        
        # Move to GPU if available
        if torch.cuda.is_available():
            inputs = inputs.to("cuda")
        
        # Generate response
        with torch.no_grad():
            outputs = model.generate(
                input_ids=inputs,
                max_new_tokens=max_tokens,
                temperature=temperature,
                top_p=0.9,
                do_sample=True,
                use_cache=True,
                pad_token_id=tokenizer.eos_token_id
            )
        
        # Decode the response
        response = tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=True)
        
        # Clean up the response
        response = response.strip()
        if not response:
            response = "I apologize, but I couldn't generate a proper response. Could you please rephrase your question about Ideon Audio products?"
        
        return response
        
    except Exception as e:
        print(f"Error generating response: {e}")
        return f"❌ I encountered an error while processing your question. Please try again. Error details: {str(e)}"

def create_interface():
    """Create the Gradio interface"""
    
    # Custom CSS for better styling
    custom_css = """
    .gradio-container {
        max-width: 1000px !important;
    }
    .header {
        text-align: center;
        background: linear-gradient(90deg, #1e3a8a, #3b82f6);
        color: white;
        padding: 20px;
        border-radius: 10px;
        margin-bottom: 20px;
    }
    .examples-row {
        margin-top: 20px;
    }
    """
    
    # Create the chat interface
    with gr.Blocks(css=custom_css, title="Ideon Audio Support Assistant") as demo:
        
        # Header
        gr.HTML("""
        <div class="header">
            <h1>🎡 Ideon Audio Technical Support Assistant</h1>
            <p>Expert knowledge for high-end audio equipment</p>
        </div>
        """)
        
        # Description
        gr.Markdown("""
        Welcome to the **Ideon Audio Technical Support Assistant**! I'm here to help you with:
        
        πŸ”Ή **Product Information** - Specifications, features, and capabilities  
        πŸ”Ή **Setup & Installation** - Connection procedures and configuration  
        πŸ”Ή **Troubleshooting** - Diagnosing and resolving technical issues  
        πŸ”Ή **Warranty Support** - Coverage details and service procedures  
        πŸ”Ή **Technical Questions** - Performance metrics and compatibility  
        
        **Supported Products**: Absolute Ξ΅ DAC, ΙΩΝ DAC, eos DAC, Absolute Stream, USB Re-clockers, and more!
        """)
        
        # Chat interface with advanced settings
        with gr.Row():
            with gr.Column(scale=3):
                chatbot = gr.ChatInterface(
                    fn=get_ideon_response,
                    chatbot=gr.Chatbot(
                        height=500,
                        placeholder="Ask me anything about Ideon Audio products...",
                        avatar_images=(None, "🎡")
                    ),
                    textbox=gr.Textbox(
                        placeholder="Type your question about Ideon Audio products here...",
                        container=False,
                        scale=7
                    ),
                    submit_btn="Ask Question",
                    retry_btn="πŸ”„ Retry",
                    undo_btn="↩️ Undo",
                    clear_btn="πŸ—‘οΈ Clear Chat",
                )
            
            with gr.Column(scale=1):
                gr.Markdown("### βš™οΈ Settings")
                
                temperature = gr.Slider(
                    minimum=0.1,
                    maximum=1.0,
                    value=0.7,
                    step=0.1,
                    label="Response Creativity",
                    info="Higher = more creative, Lower = more focused"
                )
                
                max_tokens = gr.Slider(
                    minimum=50,
                    maximum=500,
                    value=256,
                    step=50,
                    label="Response Length",
                    info="Maximum tokens in response"
                )
                
                # Update the chat function with new parameters
                def update_chat_fn(message, history):
                    return get_ideon_response(message, history, temperature.value, max_tokens.value)
                
                chatbot.fn = update_chat_fn
        
        # Example questions
        gr.Markdown("### πŸ’‘ Example Questions")
        
        example_questions = [
            "What is the recommended burn-in period for the Absolute Ξ΅ DAC?",
            "How do I connect the Absolute DAC to my audio system?",
            "My DAC won't lock onto the digital signal. What should I do?",
            "What are the main technical specifications of the Absolute Ξ΅ DAC?",
            "What's covered under the Ideon Audio warranty?",
            "How do I navigate to the General Settings screen?",
            "What digital filters are available on the DAC?",
            "What should I do if my DAC malfunctions?",
            "Can I connect the DAC directly to a power amplifier?",
            "What are the dimensions and weight of the Absolute E?"
        ]
        
        with gr.Row():
            for i in range(0, len(example_questions), 2):
                with gr.Column():
                    if i < len(example_questions):
                        gr.Examples(
                            examples=[[example_questions[i]]],
                            inputs=chatbot.textbox,
                            label=None
                        )
                    if i + 1 < len(example_questions):
                        gr.Examples(
                            examples=[[example_questions[i + 1]]],
                            inputs=chatbot.textbox,
                            label=None
                        )
        
        # Footer
        gr.Markdown("""
        ---
        
        **About this Assistant**: This AI model has been fine-tuned specifically on Ideon Audio product documentation 
        to provide expert technical support. It covers the complete product line including DACs, streamers, 
        USB re-clockers, and network optimizers.
        
        **Note**: For warranty claims or complex technical issues, please contact your authorized Ideon Audio dealer 
        or email info@ideonaudio.com directly.
        
        *Powered by Meta Llama 3.1 8B + Unsloth fine-tuning*
        """)
    
    return demo

# Create and launch the interface
if __name__ == "__main__":
    print("πŸš€ Starting Ideon Audio Support Assistant...")
    
    # Pre-load the model (optional, for faster first response)
    print("πŸ”„ Pre-loading model for faster responses...")
    load_model()
    
    # Create and launch the interface
    demo = create_interface()
    demo.launch(
        share=False,
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True
    )