Raveheart1 commited on
Commit
ec02482
·
verified ·
1 Parent(s): 85dcbec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -3
app.py CHANGED
@@ -15,14 +15,15 @@ def translate_text(tamil_text):
15
  translation = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
16
  return translation
17
 
18
- def query_gemini_api(translated_text):
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(url, headers=headers, json=payload) # Corrected this line
 
26
  if response.status_code == 200:
27
  result = response.json()
28
  creative_text = result['candidates'][0]['content']['parts'][0]['text']
@@ -38,12 +39,14 @@ def query_image(payload):
38
  return response.content
39
 
40
  def process_input(tamil_input):
 
41
  translated_output = translate_text(tamil_input)
42
- creative_output = query_gemini_api(translated_output)
43
  image_bytes = query_image({"inputs": translated_output})
44
  image = Image.open(io.BytesIO(image_bytes))
45
  return translated_output, creative_output, image
46
 
 
47
  iface = gr.Interface(
48
  fn=process_input,
49
  inputs=[gr.Textbox(label="Input Tamil Text")],
 
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']
 
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
  iface = gr.Interface(
51
  fn=process_input,
52
  inputs=[gr.Textbox(label="Input Tamil Text")],