sagar007 commited on
Commit
93dad23
·
verified ·
1 Parent(s): 4f05e78

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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=0)
7
+
8
+ # Supported languages
9
+ LANGUAGES = ["English", "Bengali", "Gujarati", "Hindi", "Kannada", "Malayalam", "Marathi", "Oriya", "Punjabi", "Tamil", "Telugu"]
10
+
11
+ def chatbot(message, history, language):
12
+ # Prepare the prompt
13
+ prompt = f"Conversation in {language}:\n"
14
+ for human, ai in history:
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()
23
+
24
+ return ai_response
25
+
26
+ # Create the Gradio interface
27
+ iface = gr.ChatInterface(
28
+ chatbot,
29
+ additional_inputs=[
30
+ gr.Dropdown(choices=LANGUAGES, label="Select Language", value="English")
31
+ ],
32
+ title="Multilingual Indian Chatbot",
33
+ description="Chat in multiple Indian languages using the sarvam-2b model.",
34
+ examples=[
35
+ ["Hello, how are you?", "English"],
36
+ ["नमस्ते, आप कैसे हैं?", "Hindi"],
37
+ ["வணக்கம், எப்படி இருக்கிறீர்கள்?", "Tamil"],
38
+ ],
39
+ theme="soft"
40
+ )
41
+
42
+ # Launch the interface
43
+ iface.launch()