sarvamai / app.py
sagar007's picture
Create app.py
93dad23 verified
raw
history blame
1.43 kB
import gradio as gr
from transformers import pipeline
# Initialize the model
model_name = "sarvamai/sarvam-2b-v0.5"
pipe = pipeline("text-generation", model=model_name, device=0)
# Supported languages
LANGUAGES = ["English", "Bengali", "Gujarati", "Hindi", "Kannada", "Malayalam", "Marathi", "Oriya", "Punjabi", "Tamil", "Telugu"]
def chatbot(message, history, language):
# Prepare the prompt
prompt = f"Conversation in {language}:\n"
for human, ai in history:
prompt += f"Human: {human}\nAI: {ai}\n"
prompt += f"Human: {message}\nAI:"
# Generate response
response = pipe(prompt, max_new_tokens=100, temperature=0.7, repetition_penalty=1.1)[0]['generated_text']
# Extract only the AI's response
ai_response = response.split("AI:")[-1].strip()
return ai_response
# Create the Gradio interface
iface = gr.ChatInterface(
chatbot,
additional_inputs=[
gr.Dropdown(choices=LANGUAGES, label="Select Language", value="English")
],
title="Multilingual Indian Chatbot",
description="Chat in multiple Indian languages using the sarvam-2b model.",
examples=[
["Hello, how are you?", "English"],
["नमस्ते, आप कैसे हैं?", "Hindi"],
["வணக்கம், எப்படி இருக்கிறீர்கள்?", "Tamil"],
],
theme="soft"
)
# Launch the interface
iface.launch()