Views
No views yet
ankitha29/telugu-colloquial-corpus]1from transformers import T5ForConditionalGeneration, T5Tokenizer
2
3model_name = "ankitha29/telugu-t5-colloquial" # Replace with your actual model name
4tokenizer = T5Tokenizer.from_pretrained(model_name)
5model = T5ForConditionalGeneration.from_pretrained(model_name)
6
7input_text = "ఏమి చేస్తున్నారు?" #What are you doing? (Telugu)
8input_ids = tokenizer(input_text, return_tensors="pt").input_ids
9
10outputs = model.generate(input_ids)
11print(tokenizer.decode(outputs[0], skip_special_tokens=True))
12Use code with caution.
13Markdown
14Limitations
15[Describe any limitations of your model. For example:]
16
17The model may not perform well on topics outside of its training data. It may also generate incorrect or nonsensical responses in some cases.
18
19License
20[Specify the license under which your model is released, e.g., Apache 2.0, MIT]
21
22Author
23[Your Name or Organization Name]
24
25**How to Prepare:**
26
271. **Replace the Placeholders:** Replace all the bracketed placeholders (`[]`) with accurate information about your model, data, and evaluation.
282. **Correct Model Class:** Ensure you use `T5ForConditionalGeneration` and `T5Tokenizer` in the code example.
293. **Hugging Face Repository:** Put this code in a hugging face repository on “Model Card”.
30Use code with caution.
31Test the Code: Verify that the code example actually works by running it yourself.
32
333. Create a Hugging Face Space (Demo):
34
35This involves creating a live, interactive demo of your chatbot using Gradio or Streamlit. A space is not necessary but does demonstrate the capabilties of the model.
36
37Here's a Gradio app code and all of your prior code:
38
39import gradio as gr
40from transformers import T5ForConditionalGeneration, T5Tokenizer
41from sentence_transformers import SentenceTransformer
42import torch
43import chromadb
44from chromadb.utils import embedding_functions
45
46# --- Load your model, tokenizer, and other components ---
47model_name = "ankitha29/telugu-t5-colloquial" # Replace this with your model name
48model = T5ForConditionalGeneration.from_pretrained(model_name)
49tokenizer = T5Tokenizer.from_pretrained(model_name)
50
51if torch.cuda.is_available():
52 device = "cuda"
53else:
54 device = "cpu"
55model = model.to(device) #set device
56
57embedding_model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-mpnet-base-v2').to(device)
58
59
60#Set up ChromaDB
61chroma_ef = embedding_functions.SentenceTransformerEmbeddingFunction(
62 model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2",
63 device=device
64)
65
66client = chromadb.Client()
67collection_name = "telugu_rag"
68collection = client.get_collection(name=collection_name, embedding_function=chroma_ef)
69# ----- Your RAG Query Function -----
70def rag_query(query, chat_history=None): #removed collection for simplification
71 """
72 Performs a RAG query and returns the chatbot's response.
73 """
74 query_lower = query.lower()
75
76 # Handle greetings and simple questions directly (without RAG)
77 if "namaskaram" in query_lower or "good morning" in query_lower or "hello" in query_lower or "hi" in query_lower: # Basic Greeting Detection
78 greeting_response = "Namaskaram! బాగున్నారా? (Baagunnaraa?) How are you?" # Telugu Greeting
79 return greeting_response
80 elif "what is your name" in query_lower or "what's your name" in query_lower:
81 name_response = "My name is Mitrabot (మిత్రబాట్)."
82 return name_response
83
84
85 # Embed the query
86 query_embedding = embedding_model.encode(query)
87
88 # Retrieve relevant chunks from ChromaDB
89 results = collection.query(
90 query_embeddings=[query_embedding],
91 n_results=3 # Number of chunks to retrieve
92 )
93
94 retrieved_chunks = results["documents"][0]
95 print(f"Retrieved Chunks: {retrieved_chunks}") # Debugging
96
97
98 # 3. Augment the query with retrieved chunks and chat history
99 context = "\n".join(retrieved_chunks)
100
101
102 # 4. Generate the answer using T5 - Chatbot Prompt
103 prompt = f"""You are a friendly Telugu chatbot named Mitrabot (మిత్రబాట్). Use the following context to answer the user's question *in Telugu*. If the context doesn't contain enough information, respond politely in Telugu saying you don't know.
104
105 Context: {context}
106
107 Question: {query}
108
109 Answer:"""
110
111 input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)
112 outputs = model.generate(input_ids,
113 max_length=256, # Limit the response length
114 num_beams=5, # Use beam search for better quality
115 no_repeat_ngram_size=2, # Avoid repetitive phrases
116 temperature=0.7, # Adjust temperature for creativity
117 early_stopping=True # Add early_stopping
118 )
119
120 answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
121
122 return answer
123
124
125# ----- Gradio Interface -----
126iface = gr.ChatInterface(
127 fn=rag_query, # Your RAG query function
128 title="Mitrabot: Your Friendly Telugu Chatbot",
129 description="Ask me anything in Telugu or English!",
130 examples=["What is a rhyme?", "Tell me a Telugu idiom"] # Example prompts
131)
132
133if __name__ == "__main__":
134 iface.launch()