pravin0077 commited on
Commit
3a6b0f7
·
verified ·
1 Parent(s): 1786f21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -33
app.py CHANGED
@@ -2,60 +2,78 @@ import requests
2
  import io
3
  from PIL import Image
4
  import gradio as gr
5
- from transformers import MarianMTModel, MarianTokenizer
6
  import os
7
 
 
8
  model_name = "Helsinki-NLP/opus-mt-mul-en"
9
- model = MarianMTModel.from_pretrained(model_name)
10
- tokenizer = MarianTokenizer.from_pretrained(model_name)
 
 
 
 
 
11
 
12
  def translate_text(tamil_text):
13
- inputs = tokenizer(tamil_text, return_tensors="pt")
14
- translated_tokens = model.generate(**inputs)
15
- translation = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
16
  return translation
17
 
18
- def query_gemini_api(translated_text, gemini_api_key):
19
- url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
20
- headers = {"Content-Type": "application/json"}
21
- prompt = f"Based on the following sentence, continue the story: {translated_text}"
22
- payload = {
23
- "contents": [{"parts": [{"text": prompt}]}]
24
- }
25
- response = requests.post(f"{url}?key={gemini_api_key}", headers=headers, json=payload)
26
-
27
- if response.status_code == 200:
28
- result = response.json()
29
- creative_text = result['candidates'][0]['content']['parts'][0]['text']
30
- return creative_text
31
- else:
32
- return f"Error: {response.status_code} - {response.text}"
33
 
34
  def query_image(payload):
35
  huggingface_api_key = os.getenv('HUGGINGFACE_API_KEY')
 
 
 
36
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
37
  headers = {"Authorization": f"Bearer {huggingface_api_key}"}
38
  response = requests.post(API_URL, headers=headers, json=payload)
39
- return response.content
40
-
41
- def process_input(tamil_input):
42
- gemini_api_key = os.getenv('GEMINI_API_KEY')
43
- translated_output = translate_text(tamil_input)
44
- creative_output = query_gemini_api(translated_output, gemini_api_key)
45
- image_bytes = query_image({"inputs": translated_output})
46
- image = Image.open(io.BytesIO(image_bytes))
47
- return translated_output, creative_output, image
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
 
50
  interface = gr.Interface(
51
  fn=process_input,
52
- inputs=[gr.Textbox(label="Input Tamil Text")],
 
 
 
53
  outputs=[
54
  gr.Textbox(label="Translated Text"),
55
  gr.Textbox(label="Creative Text"),
56
  gr.Image(label="Generated Image")
57
  ],
58
- title="TRANSART",
59
- description="Enter Tamil text to translate to English and generate an image based on the translated text."
 
 
60
  )
 
61
  interface.launch()
 
2
  import io
3
  from PIL import Image
4
  import gradio as gr
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-Neo model and tokenizer
14
+ gpt_model_name = "EleutherAI/gpt-neo-1.3B" # You can also use gpt-neo-2.7B if needed
15
+ gpt_tokenizer = AutoTokenizer.from_pretrained(gpt_model_name)
16
+ gpt_model = AutoModelForCausalLM.from_pretrained(gpt_model_name)
17
 
18
  def translate_text(tamil_text):
19
+ inputs = translation_tokenizer(tamil_text, return_tensors="pt")
20
+ translated_tokens = translation_model.generate(**inputs)
21
+ translation = translation_tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
22
  return translation
23
 
24
+ def query_gpt_neo(translated_text, max_words):
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=max_words, num_return_sequences=1)
28
+ creative_text = gpt_tokenizer.decode(outputs[0], skip_special_tokens=True)
29
+ return creative_text
 
 
 
 
 
 
 
 
 
30
 
31
  def query_image(payload):
32
  huggingface_api_key = os.getenv('HUGGINGFACE_API_KEY')
33
+ if not huggingface_api_key:
34
+ return "Error: Hugging Face API key not set."
35
+
36
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
37
  headers = {"Authorization": f"Bearer {huggingface_api_key}"}
38
  response = requests.post(API_URL, headers=headers, json=payload)
39
+
40
+ if response.status_code == 200:
41
+ return response.content
42
+ else:
43
+ return f"Error: {response.status_code} - {response.text}"
 
 
 
 
44
 
45
+ def process_input(tamil_input, max_words):
46
+ try:
47
+ # Translate the input text
48
+ translated_output = translate_text(tamil_input)
49
+
50
+ # Generate creative text using GPT-Neo
51
+ creative_output = query_gpt_neo(translated_output, max_words)
52
+
53
+ # Generate an image using Hugging Face's FLUX model
54
+ image_bytes = query_image({"inputs": translated_output})
55
+ image = Image.open(io.BytesIO(image_bytes))
56
+
57
+ return translated_output, creative_output, image
58
+ except Exception as e:
59
+ return f"Error occurred: {str(e)}", "", None
60
 
61
+ # Create a Gradio interface with interactive elements
62
  interface = gr.Interface(
63
  fn=process_input,
64
+ inputs=[
65
+ gr.Textbox(label="Input Tamil Text", placeholder="Enter your Tamil text here..."),
66
+ gr.Slider(label="Max Words for Creative Text", minimum=50, maximum=200, step=10, value=100)
67
+ ],
68
  outputs=[
69
  gr.Textbox(label="Translated Text"),
70
  gr.Textbox(label="Creative Text"),
71
  gr.Image(label="Generated Image")
72
  ],
73
+ title="TRANSART - Multimodal AI App",
74
+ description="Enter Tamil text to translate to English, generate creative text, and produce an image based on the translated text.",
75
+ theme="compact", # Use the 'compact' theme for a cleaner app look
76
+ layout="vertical" # Arrange components vertically for better readability
77
  )
78
+
79
  interface.launch()