mohantvj commited on
Commit
4c3f2c6
·
verified ·
1 Parent(s): 9362701

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -105
app.py CHANGED
@@ -1,141 +1,77 @@
1
  import gradio as gr
2
- import torch
3
- from transformers import AutoModelForCausalLM, AutoTokenizer
4
- from peft import PeftModel
5
- import os
6
-
7
- print("Loading naadini Telugu-Gemma...")
8
-
9
- token = os.environ.get("HF_TOKEN")
10
-
11
- tokenizer = AutoTokenizer.from_pretrained(
12
- "google/gemma-2b-it",
13
- token=token
14
- )
15
-
16
- base_model = AutoModelForCausalLM.from_pretrained(
17
- "google/gemma-2b-it",
18
- torch_dtype=torch.float32,
19
- device_map="cpu",
20
- token=token
21
- )
22
-
23
- model = PeftModel.from_pretrained(
24
- base_model,
25
- "mohantvj/telugu-gemma",
26
- token=token
27
- )
28
-
29
- print("Naadini loaded!")
30
-
31
 
32
  def generate_telugu(prompt, content_type):
33
- system_prompts = {
34
- "kavita (Poem)": "Write a beautiful Telugu poem about the given topic.",
35
- "paata (Song)": "Write a Telugu song with pallavi and charanam.",
36
- "katha (Story)": "Write a short Telugu story with moral.",
37
- "padyam (Classical)": "Write a classical Telugu padyam.",
38
- "sambashanana (Conversation)": "Reply naturally in Telugu.",
39
- }
40
-
41
- instruction = system_prompts.get(content_type, system_prompts["kavita (Poem)"])
42
-
43
- full_prompt = f"""### Instruction:
44
- {instruction}
45
-
46
- ### Input:
47
- {prompt}
48
-
49
- ### Response:
50
- """
51
-
52
- inputs = tokenizer(full_prompt, return_tensors="pt")
53
-
54
- with torch.no_grad():
55
- outputs = model.generate(
56
- **inputs,
57
- max_new_tokens=200,
58
- do_sample=True,
59
- temperature=0.7,
60
- top_p=0.9,
61
- repetition_penalty=1.3
62
- )
63
-
64
- response = tokenizer.decode(
65
- outputs[0][inputs.input_ids.shape[1]:],
66
- skip_special_tokens=True
67
- )
68
- return response
69
 
70
-
71
- with gr.Blocks(title="Naadini - Telugu Creative AI") as demo:
72
  gr.Markdown("""
73
- # Naadini - Telugu Creative AI
74
-
75
- Naadini means She who is the source of divine sound, poetry and wisdom
76
-
77
- Note: Running on CPU. Generation takes 3-5 minutes. Please be patient!
78
-
79
- Demo version uses Telugu-Gemma 2B.
80
- For best quality use full Naadini model locally on Apple Silicon Mac.
81
- Full model: https://huggingface.co/mohantvj/naadini-telugu
82
-
83
- Telugu lo kavithalu, paatalu, kathalu raayandi!
84
  """)
85
-
86
  with gr.Row():
87
  with gr.Column():
88
  content_type = gr.Dropdown(
89
  choices=[
90
- "kavita (Poem)",
91
- "paata (Song)",
92
- "katha (Story)",
93
- "padyam (Classical)",
94
- "sambashanana (Conversation)"
95
  ],
96
- value="kavita (Poem)",
97
  label="What to Write"
98
  )
99
  prompt = gr.Textbox(
100
- placeholder="varsham gurinchi oka kavita raayandi...",
101
  label="Your Prompt",
102
  lines=3
103
  )
104
- generate_btn = gr.Button(
105
- "Generate / Raayandi",
106
- variant="primary"
107
- )
108
-
109
  with gr.Column():
110
  output = gr.Textbox(
111
- label="Naadini Response",
112
  lines=15
113
  )
114
-
115
  gr.Examples(
116
  examples=[
117
- ["varsham gurinchi oka andhamaina kavita raayandi", "kavita (Poem)"],
118
- ["amma gurinchi oka paata raayandi", "paata (Song)"],
119
- ["pillala kosam oka chinna katha raayandi", "katha (Story)"],
120
- ["vemana shaililo ahankaram gurinchi padyam raayandi", "padyam (Classical)"],
121
- ["namaskaram! meeru ela unnaru?", "sambashanana (Conversation)"],
122
  ],
123
  inputs=[prompt, content_type]
124
  )
125
-
126
  generate_btn.click(
127
  fn=generate_telugu,
128
  inputs=[prompt, content_type],
129
  outputs=output
130
  )
131
-
132
  gr.Markdown("""
133
  ---
134
- Built with love by Thonangi Venkata Jagan Mohan
135
-
136
- Full model: https://huggingface.co/mohantvj/naadini-telugu
137
-
138
- For faster generation on Apple Silicon Mac install mlx-lm
 
 
 
 
 
 
 
 
 
 
139
  """)
140
 
141
  demo.launch()
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  def generate_telugu(prompt, content_type):
4
+ return f"Coming soon! నాదిని is being set up...\n\nYour prompt: {prompt}\nType: {content_type}\n\nPlease visit: https://huggingface.co/mohantvj/naadini-telugu for usage instructions."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ with gr.Blocks(title="నాదిని - Telugu Creative AI") as demo:
 
7
  gr.Markdown("""
8
+ # 🎵 నాదిని (Naadini) - Telugu Creative AI
9
+ **నాదిని** means *"She who is the source of divine sound, poetry and wisdom"*
10
+
11
+ ⚠️ Full model requires Apple Silicon Mac.
12
+ See [model page](https://huggingface.co/mohantvj/naadini-telugu) for installation.
13
+
14
+ తెలుగులో కవితలు, పాటలు, కథలు రాయండి!
 
 
 
 
15
  """)
16
+
17
  with gr.Row():
18
  with gr.Column():
19
  content_type = gr.Dropdown(
20
  choices=[
21
+ "కవిత (Poem)",
22
+ "పాట (Song)",
23
+ "కథ (Story)",
24
+ "పద్యం (Classical)",
25
+ "సంభాషణ (Conversation)"
26
  ],
27
+ value="కవిత (Poem)",
28
  label="What to Write"
29
  )
30
  prompt = gr.Textbox(
31
+ placeholder="వర్షం గురించి ఒక కవిత రాయండి...",
32
  label="Your Prompt",
33
  lines=3
34
  )
35
+ generate_btn = gr.Button("✨ Generate", variant="primary")
36
+
 
 
 
37
  with gr.Column():
38
  output = gr.Textbox(
39
+ label="నాదిని's Response",
40
  lines=15
41
  )
42
+
43
  gr.Examples(
44
  examples=[
45
+ ["వర్షం గురించి ఒక అందమైన కవిత రాయండి", "కవిత (Poem)"],
46
+ ["అమ్మ గురించి ఒక పాట రాయండి", "పాట (Song)"],
47
+ ["పిల్లల కోసం ఒక చిన్న కథ ర��యండి", "కథ (Story)"],
 
 
48
  ],
49
  inputs=[prompt, content_type]
50
  )
51
+
52
  generate_btn.click(
53
  fn=generate_telugu,
54
  inputs=[prompt, content_type],
55
  outputs=output
56
  )
57
+
58
  gr.Markdown("""
59
  ---
60
+ Built with ❤️ by **Thonangi Venkata Jagan Mohan**
61
+
62
+ ## How to Use Locally
63
+ ```bash
64
+ pip install mlx-lm
65
+ ```
66
+ ```python
67
+ from mlx_lm import load, generate
68
+ from mlx_lm.sample_utils import make_sampler
69
+
70
+ model, tokenizer = load(
71
+ "mlx-community/Qwen3-14B-4bit",
72
+ adapter_path="mohantvj/naadini-telugu"
73
+ )
74
+ ```
75
  """)
76
 
77
  demo.launch()