deepsync commited on
Commit
44102fe
1 Parent(s): 72906d6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+
4
+ import google.auth
5
+ from google.auth.transport.requests import Request
6
+
7
+ def get_google_token():
8
+ credentials, project = google.auth.load_credentials_from_dict(
9
+ json.loads(os.environ.get('GCP_FINETUNE_KEY')),
10
+ scopes=[
11
+ "https://www.googleapis.com/auth/cloud-platform",
12
+ "https://www.googleapis.com/auth/generative-language.tuning",
13
+ ],
14
+ )
15
+ request = Request()
16
+ credentials.refresh(request)
17
+ access_token = credentials.token
18
+ return access_token
19
+
20
+
21
+ def dubpro_english_transliteration(text):
22
+ API_URL = os.environ.get("GEMINI_FINETUNED_HINDI_ENG_API")
23
+ BEARER_TOKEN = get_google_token()
24
+ headers = {
25
+ "Authorization": f"Bearer {BEARER_TOKEN}",
26
+ "Content-Type": "application/json",
27
+ }
28
+ payload = {
29
+ "contents": [
30
+ {
31
+ "parts": [{"text": f"input: {text}"}],
32
+ "role": "user",
33
+ }
34
+ ],
35
+ "generationConfig": {
36
+ "maxOutputTokens": 8192,
37
+ "temperature": 0.85,
38
+ },
39
+ "safetySettings": [
40
+ {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
41
+ {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
42
+ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
43
+ {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
44
+ ],
45
+ }
46
+ result = requests.post(
47
+ url=API_URL,
48
+ headers=headers,
49
+ json=payload
50
+ )
51
+ response = result.json()
52
+ response_content = response['candidates'][0]['content']['parts'][0]['text'].replace("output:", "").strip().replace("'text':", "").replace("{", "").replace("}", "").strip().strip("'").strip('"')
53
+ return response_content
54
+
55
+
56
+ with gr.Blocks() as demo:
57
+ gr.Markdown("English Transliteration Tool")
58
+ with gr.Row():
59
+ input_text = gr.Textbox(label="Input text", info="Please enter English text.")
60
+ output_text = gr.Textbox(label="Output text")
61
+ submit = gr.Button("Submit")
62
+ submit.click(dubpro_english_transliteration, input_text, output_text)
63
+
64
+ demo.launch()