rahul7star commited on
Commit
17c4ee5
·
verified ·
1 Parent(s): 75d11f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -4
app.py CHANGED
@@ -20,7 +20,7 @@ model = GPT2LMHeadModel.from_pretrained(model_name)
20
  # model = AutoModelForCausalLM.from_pretrained(model_name)
21
 
22
  # Ensure model uses the correct pad token
23
- model.config.pad_token_id = tokenizer.pad_token_id
24
 
25
  # 2️⃣ Function to Generate Hindi Poetry
26
  def generate_poetry_base(prompt, max_length=100, temperature=0.7, top_k=50, top_p=0.95):
@@ -55,12 +55,15 @@ def generate_poetry(prompt, max_length=100, temperature=0.7, top_k=50, top_p=0.9
55
  return tokenizer.decode(output[0], skip_special_tokens=True)
56
 
57
 
 
 
 
58
  def generate_random_poem(num_lines=4, max_length=150, temperature=1.0, top_p=0.9):
59
  # Randomly select a line from the dataset
60
  random_line = random.choice(dataset["poem"])
61
 
62
  # Prepare the input text with the random line selected, and start with a unique phrase to avoid repetition
63
- input_text = f"{random_line} " # Unique start to force variety
64
 
65
  # Tokenize the input text
66
  encoding = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True, max_length=max_length)
@@ -110,10 +113,14 @@ def generate_random_poem(num_lines=4, max_length=150, temperature=1.0, top_p=0.9
110
 
111
 
112
 
 
 
 
 
113
  # 3️⃣ Gradio Interface
114
  interface = gr.Interface(
115
- fn=generate_poetry,
116
- #fn=generate_random_poem(num_lines=4),
117
  inputs=[
118
  gr.Textbox(label="Enter Prompt", placeholder="Start your Hindi poem..."),
119
  gr.Slider(50, 500, step=10, value=100, label="Max Length"),
 
20
  # model = AutoModelForCausalLM.from_pretrained(model_name)
21
 
22
  # Ensure model uses the correct pad token
23
+ #model.config.pad_token_id = tokenizer.pad_token_id
24
 
25
  # 2️⃣ Function to Generate Hindi Poetry
26
  def generate_poetry_base(prompt, max_length=100, temperature=0.7, top_k=50, top_p=0.95):
 
55
  return tokenizer.decode(output[0], skip_special_tokens=True)
56
 
57
 
58
+
59
+
60
+ # Poetry Generation Function with Random Selection from Dataset and Explicit 4-Line Structure
61
  def generate_random_poem(num_lines=4, max_length=150, temperature=1.0, top_p=0.9):
62
  # Randomly select a line from the dataset
63
  random_line = random.choice(dataset["poem"])
64
 
65
  # Prepare the input text with the random line selected, and start with a unique phrase to avoid repetition
66
+ input_text = f"मैया मोरी {random_line} " # Unique start to force variety
67
 
68
  # Tokenize the input text
69
  encoding = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True, max_length=max_length)
 
113
 
114
 
115
 
116
+
117
+
118
+
119
+
120
  # 3️⃣ Gradio Interface
121
  interface = gr.Interface(
122
+ #fn=generate_poetry,
123
+ fn=generate_random_poem(num_lines=4),
124
  inputs=[
125
  gr.Textbox(label="Enter Prompt", placeholder="Start your Hindi poem..."),
126
  gr.Slider(50, 500, step=10, value=100, label="Max Length"),