Raveheart1 commited on
Commit
ca60315
·
verified ·
1 Parent(s): 0ed676c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import io
3
+ from PIL import Image
4
+ import gradio as gr
5
+ from transformers import MarianMTModel, MarianTokenizer
6
+
7
+ # Load the translation model
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
+ # Function to translate Tamil text to English
13
+ def translate_text(tamil_text):
14
+ inputs = tokenizer(tamil_text, return_tensors="pt")
15
+ translated_tokens = model.generate(**inputs)
16
+ translation = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
17
+ return translation
18
+
19
+ # Function to query Gemini API for creative text
20
+ def query_gemini_api(translated_text, gemini_api_key):
21
+ url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
22
+ headers = {"Content-Type": "application/json"}
23
+ payload = {
24
+ "contents": [{"parts": [{"text": translated_text}]}]
25
+ }
26
+ response = requests.post(f"{url}?key={gemini_api_key}", headers=headers, json=payload)
27
+
28
+ if response.status_code == 200:
29
+ result = response.json()
30
+ creative_text = result['candidates'][0]['content']['parts'][0]['text']
31
+ return creative_text
32
+ else:
33
+ return f"Error: {response.status_code} - {response.text}"
34
+
35
+ # Function to query the Hugging Face API for image generation
36
+ def query_image(payload, huggingface_api_key):
37
+ API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
38
+ headers = {"Authorization": f"Bearer {huggingface_api_key}"}
39
+ response = requests.post(API_URL, headers=headers, json=payload)
40
+ return response.content
41
+
42
+ # Main function to process input
43
+ def process_input(tamil_input, gemini_api_key, huggingface_api_key):
44
+ translated_output = translate_text(tamil_input)
45
+ creative_output = query_gemini_api(translated_output, gemini_api_key)
46
+ image_bytes = query_image({"inputs": translated_output}, huggingface_api_key)
47
+ image = Image.open(io.BytesIO(image_bytes))
48
+ return translated_output, creative_output, image
49
+
50
+ # Gradio interface
51
+ iface = gr.Interface(
52
+ fn=process_input,
53
+ inputs=[
54
+ gr.Textbox(label="Input Tamil Text"),
55
+ gr.Textbox(label="Enter your Gemini API Key", type="password"),
56
+ gr.Textbox(label="Enter your Hugging Face API Key", type="password")
57
+ ],
58
+ outputs=[
59
+ gr.Textbox(label="Translated Text"),
60
+ gr.Textbox(label="Creative Text"),
61
+ gr.Image(label="Generated Image")
62
+ ],
63
+ title="TRANSART🎨 BY Sakthi",
64
+ description="Enter Tamil text to translate to English and generate an image based on the translated text."
65
+ )
66
+
67
+ iface.launch()