File size: 2,205 Bytes
d613631
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

# Load AI model
print("Loading model...")
try:
    generator = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1", device=-1)
except:
    # Fallback to smaller model
    generator = pipeline("text-generation", model="gpt2")

def chat(user_message):
    """Chat with AI model"""
    try:
        # Format message for better responses
        prompt = f"User: {user_message}\nAssistant:"
        
        response = generator(
            prompt,
            max_length=300,
            num_return_sequences=1,
            temperature=0.7,
            top_p=0.9,
            do_sample=True
        )
        
        # Extract and clean response
        generated_text = response[0]["generated_text"]
        # Remove the prompt from response
        assistant_response = generated_text.split("Assistant:")[-1].strip()
        
        return assistant_response if assistant_response else "I couldn't generate a response."
        
    except Exception as e:
        return f"Error: {str(e)}"

# Create the EXACT same UI as your Kaggle version
with gr.Blocks(title="Mini ChatBot", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# Mini ChatBot")
    gr.Markdown("Powered by AI models - Works without local LLM downloads")
    
    with gr.Row():
        with gr.Column():
            user_input = gr.Textbox(
                label="Your Question",
                placeholder="Ask me anything...",
                lines=3,
                scale=1
            )
            submit_btn = gr.Button("Send", scale=1, variant="primary")
        
        with gr.Column():
            output = gr.Textbox(
                label="Response",
                lines=5,
                scale=1,
                interactive=False
            )
    
    # Connect button
    submit_btn.click(chat, inputs=user_input, outputs=output)
    
    # Add example questions
    gr.Examples(
        examples=[
            "What is machine learning?",
            "Explain quantum computing",
            "How does AI work?",
            "What is deep learning?"
        ],
        inputs=user_input
    )

if __name__ == "__main__":
    demo.launch()