sagar007 commited on
Commit
8d76cd2
1 Parent(s): e2d8960

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -7
app.py CHANGED
@@ -1,9 +1,11 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
 
4
- # Initialize the model
5
  model_name = "sarvamai/sarvam-2b-v0.5"
6
- pipe = pipeline("text-generation", model=model_name, device=-1) # Set device to -1 for CPU
 
7
 
8
  # Supported languages
9
  LANGUAGES = ["English", "Bengali", "Gujarati", "Hindi", "Kannada", "Malayalam", "Marathi", "Oriya", "Punjabi", "Tamil", "Telugu"]
@@ -15,8 +17,20 @@ def chatbot(message, history, language):
15
  prompt += f"Human: {human}\nAI: {ai}\n"
16
  prompt += f"Human: {message}\nAI:"
17
 
18
- # Generate response
19
- response = pipe(prompt, max_new_tokens=100, temperature=0.7, repetition_penalty=1.1)[0]['generated_text']
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # Extract only the AI's response
22
  ai_response = response.split("AI:")[-1].strip()
@@ -32,10 +46,20 @@ iface = gr.ChatInterface(
32
  title="Multilingual Indian Chatbot",
33
  description="Chat in multiple Indian languages using the sarvam-2b model.",
34
  examples=[
35
- ["नमस्ते, आप कैसे हैं?", "Hindi"]
 
 
 
 
 
 
 
 
 
 
36
  ],
37
  theme="soft"
38
  )
39
 
40
  # Launch the interface
41
- iface.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
 
5
+ # Initialize the model and tokenizer
6
  model_name = "sarvamai/sarvam-2b-v0.5"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
9
 
10
  # Supported languages
11
  LANGUAGES = ["English", "Bengali", "Gujarati", "Hindi", "Kannada", "Malayalam", "Marathi", "Oriya", "Punjabi", "Tamil", "Telugu"]
 
17
  prompt += f"Human: {human}\nAI: {ai}\n"
18
  prompt += f"Human: {message}\nAI:"
19
 
20
+ # Tokenize and generate
21
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
22
+
23
+ with torch.no_grad():
24
+ outputs = model.generate(
25
+ **inputs,
26
+ max_new_tokens=100,
27
+ temperature=0.7,
28
+ repetition_penalty=1.1,
29
+ do_sample=True,
30
+ pad_token_id=tokenizer.eos_token_id
31
+ )
32
+
33
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
34
 
35
  # Extract only the AI's response
36
  ai_response = response.split("AI:")[-1].strip()
 
46
  title="Multilingual Indian Chatbot",
47
  description="Chat in multiple Indian languages using the sarvam-2b model.",
48
  examples=[
49
+ ["Hello, how are you?", "English"],
50
+ ["नमस्ते, आप कैसे हैं?", "Hindi"],
51
+ ["வணக்கம், எப்படி இருக்கிறீர்கள்?", "Tamil"],
52
+ ["ନମସ୍କାର, ଆପଣ କେମିତି ଅଛନ୍ତି?", "Oriya"],
53
+ ["નમસ્તે, તમે કેમ છો?", "Gujarati"],
54
+ ["নমস্কার, আপনি কেমন আছেন?", "Bengali"],
55
+ ["ನಮಸ್ಕಾರ, ನೀವು ಹೇಗಿದ್ದೀರಿ?", "Kannada"],
56
+ ["നമസ്കാരം, സുഖമാണോ?", "Malayalam"],
57
+ ["नमस्कार, तुम्ही कसे आहात?", "Marathi"],
58
+ ["ਸਤ ਸ੍ਰੀ ਅਕਾਲ, ਤੁਸੀਂ ਕਿਵੇਂ ਹੋ?", "Punjabi"],
59
+ ["నమస్కారం, మీరు ఎలా ఉన్నారు?", "Telugu"]
60
  ],
61
  theme="soft"
62
  )
63
 
64
  # Launch the interface
65
+ iface.launch()