pravin0077 commited on
Commit
2f1d0aa
·
verified ·
1 Parent(s): 3f10588

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -5,12 +5,13 @@ import gradio as gr
5
  from transformers import MarianMTModel, MarianTokenizer, AutoModelForCausalLM, AutoTokenizer
6
  import os
7
 
8
- # Load models and tokenizers globally to avoid reloading them for every request
9
  model_name = "Helsinki-NLP/opus-mt-mul-en"
10
  translation_model = MarianMTModel.from_pretrained(model_name)
11
  translation_tokenizer = MarianTokenizer.from_pretrained(model_name)
12
 
13
- gpt_model_name = "EleutherAI/gpt-neo-1.3B"
 
14
  gpt_tokenizer = AutoTokenizer.from_pretrained(gpt_model_name)
15
  gpt_model = AutoModelForCausalLM.from_pretrained(gpt_model_name)
16
 
@@ -20,10 +21,10 @@ def translate_text(tamil_text):
20
  translation = translation_tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
21
  return translation
22
 
23
- def query_gpt_neo(translated_text):
24
  prompt = f"Continue the story based on the following text: {translated_text}"
25
  inputs = gpt_tokenizer(prompt, return_tensors="pt")
26
- outputs = gpt_model.generate(inputs['input_ids'], max_length=50, num_return_sequences=1) # Reduced max_length
27
  creative_text = gpt_tokenizer.decode(outputs[0], skip_special_tokens=True)
28
  return creative_text
29
 
@@ -46,8 +47,8 @@ def process_input(tamil_input):
46
  # Translate the input text
47
  translated_output = translate_text(tamil_input)
48
 
49
- # Generate creative text using GPT-Neo
50
- creative_output = query_gpt_neo(translated_output)
51
 
52
  # Generate an image using Hugging Face's FLUX model
53
  image_bytes = query_image({"inputs": translated_output})
 
5
  from transformers import MarianMTModel, MarianTokenizer, AutoModelForCausalLM, AutoTokenizer
6
  import os
7
 
8
+ # Load the translation model
9
  model_name = "Helsinki-NLP/opus-mt-mul-en"
10
  translation_model = MarianMTModel.from_pretrained(model_name)
11
  translation_tokenizer = MarianTokenizer.from_pretrained(model_name)
12
 
13
+ # Load GPT-2 model and tokenizer (smaller and faster than GPT-Neo)
14
+ gpt_model_name = "gpt2"
15
  gpt_tokenizer = AutoTokenizer.from_pretrained(gpt_model_name)
16
  gpt_model = AutoModelForCausalLM.from_pretrained(gpt_model_name)
17
 
 
21
  translation = translation_tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
22
  return translation
23
 
24
+ def query_gpt_2(translated_text):
25
  prompt = f"Continue the story based on the following text: {translated_text}"
26
  inputs = gpt_tokenizer(prompt, return_tensors="pt")
27
+ outputs = gpt_model.generate(inputs['input_ids'], max_length=50, num_return_sequences=1) # Reduced max_length for speed
28
  creative_text = gpt_tokenizer.decode(outputs[0], skip_special_tokens=True)
29
  return creative_text
30
 
 
47
  # Translate the input text
48
  translated_output = translate_text(tamil_input)
49
 
50
+ # Generate creative text using GPT-2
51
+ creative_output = query_gpt_2(translated_output)
52
 
53
  # Generate an image using Hugging Face's FLUX model
54
  image_bytes = query_image({"inputs": translated_output})