File size: 2,947 Bytes
44d9c91
f84087d
 
44d9c91
f84087d
 
 
 
 
 
 
 
44d9c91
f84087d
44d9c91
f84087d
 
44d9c91
94a5011
 
f84087d
 
 
 
94a5011
 
 
 
 
 
f84087d
94a5011
 
f84087d
 
94a5011
f84087d
 
94a5011
f84087d
 
 
94a5011
f84087d
 
 
 
 
 
 
94a5011
f84087d
44d9c91
94a5011
44d9c91
 
94a5011
 
f84087d
 
94a5011
 
 
 
f84087d
44d9c91
f84087d
94a5011
f84087d
44d9c91
94a5011
f84087d
 
44d9c91
f84087d
44d9c91
 
 
f84087d
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# Load Anki-2.5 model
model_name = "anktechsol/anki-2.5"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype=torch.float32,
    device_map="auto"
)

def respond(message, history, system_message, max_tokens, temperature, top_p):
    """
    Indian Language Q&A powered by Anki-2.5
    Supports Hindi, Tamil, Bengali, Telugu, and other Indian languages
    """
    # Build prompt manually
    prompt = system_message + "\n\n"
    
    # Add history
    for msg in history:
        if isinstance(msg, dict):
            role = msg.get("role", "")
            content = msg.get("content", "")
            if role == "user":
                prompt += f"User: {content}\n"
            elif role == "assistant":
                prompt += f"Assistant: {content}\n"
        elif len(msg) == 2:
            prompt += f"User: {msg[0]}\n"
            prompt += f"Assistant: {msg[1]}\n"
    
    # Add current message
    prompt += f"User: {message}\nAssistant:"
    
    # Generate response
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    
    with torch.no_grad():
        outputs = model.generate(
            inputs["input_ids"],
            max_new_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
            do_sample=True,
            pad_token_id=tokenizer.eos_token_id
        )
    
    response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
    return response

# Create ChatInterface for multilingual Q&A
chatbot = gr.ChatInterface(
    respond,
    title="🚀 Indian Language Q&A Assistant - Powered by Anki-2.5",
    description="Ask questions in Hindi, Tamil, Bengali, Telugu, and other Indian languages!",
    examples=[
        ["भारत की राजधानी क्या है?"],
        ["What is the capital of India?"],
        ["ভারতের স্বাধীনতা দিবস কবে?"],
        ["தமிழ் மொழியில் எண்ணிக்கையில் பேசுவது எப்படி?"],
        ["Explain the importance of education"],
    ],
    additional_inputs=[
        gr.Textbox(
            value="You are a helpful multilingual assistant that can answer questions in multiple Indian languages including Hindi, English, Bengali, Tamil, Telugu, and others. Provide accurate and helpful responses.",
            label="System Message"
        ),
        gr.Slider(minimum=50, maximum=1024, value=512, step=1, label="Max Tokens"),
        gr.Slider(minimum=0.1, maximum=1.5, value=0.7, step=0.1, label="Temperature"),
        gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
    ],
    theme=gr.themes.Soft(),
)

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