File size: 3,997 Bytes
05f1d97
 
 
da466c6
05f1d97
eeae524
05f1d97
 
6fe40f1
05f1d97
eeae524
05f1d97
eeae524
da466c6
 
 
 
 
 
 
 
6fe40f1
eeae524
da466c6
 
 
 
 
 
 
6fe40f1
eeae524
da466c6
 
 
 
6fe40f1
eeae524
da466c6
eeae524
da466c6
 
eeae524
 
 
da466c6
 
eeae524
 
 
 
 
 
 
da466c6
 
eeae524
 
 
 
 
da466c6
05f1d97
eeae524
da466c6
 
 
 
 
 
eeae524
da466c6
 
 
eeae524
da466c6
 
eeae524
da466c6
eeae524
 
 
 
da466c6
eeae524
da466c6
 
05f1d97
da466c6
eeae524
 
 
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import re

# Load model and tokenizer
model_name = "microsoft/DialoGPT-small"
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(model_name)
model.config.pad_token_id = tokenizer.eos_token_id  # Ensure no pad token issues

# System prompt for company context
SYSTEM_PROMPT = """You are MediaDecode's AI assistant, a professional IT services and software development company. 
Provide concise, technical yet friendly answers about:
- Web/Mobile Development (Java, PHP, .NET, C++)
- Product Design & UI/UX
- Staff Augmentation
- Pricing Plans ($129.99-$189.99/month)
- Contact: +1 (647) 928-8768 | info@mediadecode.com
If unsure, direct users to contact sales/support."""

# FAQ patterns
FAQ_RESPONSES = {
    r"(?i)(contact|phone|email|address)": "πŸ“ž **Contact Us:**\n- Canada: +1 (647) 928-8768\n- Pakistan: +92 307 5090706\nβœ‰οΈ **Email:** info@mediadecode.com\nπŸ“ **Addresses:** [See our website](https://mediadecode.com)",
    r"(?i)(service|offer|provide)": "πŸ›  **Our Services:**\n1. Custom Software Development\n2. Web/Mobile Apps (Java/PHP/.NET)\n3. UI/UX Design\n4. Staff Augmentation\n5. AI/ML Solutions\n\nπŸ’‘ See all: [Services Page](https://mediadecode.com/services)",
    r"(?i)(price|plan|cost)": "πŸ’° **Pricing Plans:**\n- Basic: $129.99/mo\n- Economy: $159.99/mo\n- Premium: $189.99/mo\n\nIncludes consulting, 24/7 monitoring, and security. [Learn more](https://mediadecode.com/pricing)",
    r"(?i)(portfolio|projects|showcase)": "🎨 **Portfolio:** We've delivered 100+ projects in healthcare, e-commerce, and more. Explore: [Our Showcase](https://mediadecode.com/showcase)",
    r"(?i)(technology|tech stack|tools)": "βš™οΈ **Technologies:**\n- Web: Java, PHP, .NET, C++\n- Mobile: iOS, Android\n- AI/ML, IoT, Wearables\n\nπŸ”— Full list: [Tech Stack](https://mediadecode.com/technology)"
}

# Fallback message
DEFAULT_RESPONSE = """πŸ€– I couldn't find a specific answer. For detailed inquiries:
- Call: +1 (647) 928-8768
- Email: info@mediadecode.com
- Visit: [Support Page](https://mediadecode.com/contact)"""

# Response generator
def respond(message, chat_history):
    # Check for predefined FAQs
    for pattern, response in FAQ_RESPONSES.items():
        if re.search(pattern, message):
            return f"{response}\n\nHow else can I help?"

    # Generate AI response
    prompt = f"{SYSTEM_PROMPT}\nUser: {message}\nAI:"
    inputs = tokenizer(prompt, return_tensors="pt")
    with torch.no_grad():
        outputs = model.generate(
            inputs.input_ids,
            max_length=200,
            pad_token_id=tokenizer.eos_token_id,
            temperature=0.7
        )
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    response = response.split("AI:")[-1].strip()

    # Check for fallback conditions
    if not response or len(response) < 5 or "i don't know" in response.lower():
        return DEFAULT_RESPONSE

    return response

# Gradio interface
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # πŸš€ MediaDecode Support Bot
    **Ask about:** Services | Pricing | Technologies | Contact  
    *Powered by AI + Company Knowledge Base*
    """)

    chatbot = gr.Chatbot(height=400)
    msg = gr.Textbox(label="Your Query", placeholder="e.g., What's your pricing for Java development?")
    clear = gr.Button("Clear Chat")

    def user(user_message, history):
        return "", history + [[user_message, None]]

    def bot(history):
        if not history or not history[-1][0]:
            return history
        bot_reply = respond(history[-1][0], history[:-1])
        history[-1][1] = bot_reply
        return history

    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, chatbot, chatbot
    )

    clear.click(lambda: [], None, chatbot, queue=False)

demo.launch()