anktechsol commited on
Commit
94a5011
·
verified ·
1 Parent(s): 08f764f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -22
app.py CHANGED
@@ -16,31 +16,31 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
16
  Indian Language Q&A powered by Anki-2.5
17
  Supports Hindi, Tamil, Bengali, Telugu, and other Indian languages
18
  """
19
- # Construct conversation with system message
20
- conversation = [{"role": "system", "content": system_message}]
21
 
22
  # Add history
23
  for msg in history:
24
  if isinstance(msg, dict):
25
- conversation.append(msg)
 
 
 
 
 
26
  elif len(msg) == 2:
27
- conversation.append({"role": "user", "content": msg[0]})
28
- conversation.append({"role": "assistant", "content": msg[1]})
29
 
30
  # Add current message
31
- conversation.append({"role": "user", "content": message})
32
 
33
  # Generate response
34
- inputs = tokenizer.apply_chat_template(
35
- conversation,
36
- tokenize=True,
37
- add_generation_prompt=True,
38
- return_tensors="pt"
39
- ).to(model.device)
40
 
41
  with torch.no_grad():
42
  outputs = model.generate(
43
- inputs,
44
  max_new_tokens=max_tokens,
45
  temperature=temperature,
46
  top_p=top_p,
@@ -48,26 +48,27 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
48
  pad_token_id=tokenizer.eos_token_id
49
  )
50
 
51
- response = tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=True)
52
  return response
53
 
54
- # Create ChatInterface with Indian language focus
55
  chatbot = gr.ChatInterface(
56
  respond,
57
- title="🇮🇳 Indian Language Q&A Assistant - Powered by Anki-2.5",
58
- description="Ask questions in Hindi, Tamil, Bengali, Telugu, Urdu, Gujarati, and other Indian languages. This assistant understands Indian cultural context and regional nuances.",
59
  examples=[
60
  ["भारत की राजधानी क्या है?"],
61
- ["இந்தியாவில் எத்தனை மாநிலங்கள் உள்ளன?"],
62
- ["ভারতের সবচেয়ে বড় শহর কোনটি?"],
63
- ["What are the major festivals celebrated in India?"],
 
64
  ],
65
  additional_inputs=[
66
  gr.Textbox(
67
- value="You are a helpful assistant specializing in Indian languages and culture. You understand Hindi, Tamil, Bengali, Telugu, and other Indian languages. Respond accurately and culturally appropriately.",
68
  label="System Message"
69
  ),
70
- gr.Slider(minimum=50, maximum=1024, value=256, step=1, label="Max Tokens"),
71
  gr.Slider(minimum=0.1, maximum=1.5, value=0.7, step=0.1, label="Temperature"),
72
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
73
  ],
 
16
  Indian Language Q&A powered by Anki-2.5
17
  Supports Hindi, Tamil, Bengali, Telugu, and other Indian languages
18
  """
19
+ # Build prompt manually
20
+ prompt = system_message + "\n\n"
21
 
22
  # Add history
23
  for msg in history:
24
  if isinstance(msg, dict):
25
+ role = msg.get("role", "")
26
+ content = msg.get("content", "")
27
+ if role == "user":
28
+ prompt += f"User: {content}\n"
29
+ elif role == "assistant":
30
+ prompt += f"Assistant: {content}\n"
31
  elif len(msg) == 2:
32
+ prompt += f"User: {msg[0]}\n"
33
+ prompt += f"Assistant: {msg[1]}\n"
34
 
35
  # Add current message
36
+ prompt += f"User: {message}\nAssistant:"
37
 
38
  # Generate response
39
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
 
 
 
 
40
 
41
  with torch.no_grad():
42
  outputs = model.generate(
43
+ inputs["input_ids"],
44
  max_new_tokens=max_tokens,
45
  temperature=temperature,
46
  top_p=top_p,
 
48
  pad_token_id=tokenizer.eos_token_id
49
  )
50
 
51
+ response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
52
  return response
53
 
54
+ # Create ChatInterface for multilingual Q&A
55
  chatbot = gr.ChatInterface(
56
  respond,
57
+ title="🚀 Indian Language Q&A Assistant - Powered by Anki-2.5",
58
+ description="Ask questions in Hindi, Tamil, Bengali, Telugu, and other Indian languages!",
59
  examples=[
60
  ["भारत की राजधानी क्या है?"],
61
+ ["What is the capital of India?"],
62
+ ["ভারতের স্বাধীনতা দিবস কবে?"],
63
+ ["தமிழ் மொழியில் எண்ணிக்கையில் பேசுவது எப்படி?"],
64
+ ["Explain the importance of education"],
65
  ],
66
  additional_inputs=[
67
  gr.Textbox(
68
+ 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.",
69
  label="System Message"
70
  ),
71
+ gr.Slider(minimum=50, maximum=1024, value=512, step=1, label="Max Tokens"),
72
  gr.Slider(minimum=0.1, maximum=1.5, value=0.7, step=0.1, label="Temperature"),
73
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
74
  ],