sachin commited on
Commit
4e1fde2
·
1 Parent(s): bf7c069
Files changed (2) hide show
  1. app.py +120 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ import os
5
+ import dwani
6
+ import logging
7
+
8
+ # Set up logging
9
+ logging.basicConfig(level=logging.INFO)
10
+ logger = logging.getLogger(__name__)
11
+
12
+ # Configure dwani API settings
13
+ dwani.api_key = os.getenv("DWANI_API_KEY")
14
+ dwani.api_base = os.getenv("DWANI_API_BASE_URL")
15
+
16
+ # Log API configuration for debugging
17
+ logger.debug("DWANI_API_KEY: %s", "Set" if dwani.api_key else "Not set")
18
+ logger.debug("DWANI_API_BASE_URL: %s", dwani.api_base)
19
+
20
+ # List of supported languages
21
+ LANGUAGES = [
22
+ "Assamese", "Punjabi",
23
+ "Bengali", "Malayalam ",
24
+ "English", "Marathi ", "Tamil",
25
+ "Gujarati", "Telugu ",
26
+ "Hindi", "Kannada", "Odia "
27
+ ]
28
+
29
+ def translate_api(sentences, src_lang, tgt_lang):
30
+ logger.debug("Received inputs - Sentences: %s, Source Lang: %s, Target Lang: %s", sentences, src_lang, tgt_lang)
31
+
32
+ # Convert simple string input to list
33
+ try:
34
+ if isinstance(sentences, str):
35
+ # Split on commas and strip whitespace
36
+ sentences = [s.strip() for s in sentences.split(",") if s.strip()]
37
+ elif isinstance(sentences, list):
38
+ # Ensure all elements are strings and non-empty
39
+ sentences = [s.strip() for s in sentences if isinstance(s, str) and s.strip()]
40
+ else:
41
+ logger.error("Invalid input type for sentences: %s", type(sentences))
42
+ return {"error": "Invalid input: sentences must be a string or list of strings"}
43
+ except Exception as e:
44
+ logger.error("Error processing sentences input: %s", str(e))
45
+ return {"error": f"Error processing input: {str(e)}"}
46
+
47
+ # Validate sentences content
48
+ if not sentences:
49
+ logger.error("No valid sentences provided: %s", sentences)
50
+ return {"error": "Please provide at least one non-empty sentence"}
51
+
52
+ # Extract language codes
53
+ src_lang_code = (src_lang)
54
+ tgt_lang_code = (tgt_lang)
55
+
56
+ if not src_lang_code or not tgt_lang_code:
57
+ logger.error("Invalid language codes - Source: %s, Target: %s", src_lang_code, tgt_lang_code)
58
+ return {"error": "Invalid source or target language selection"}
59
+
60
+ logger.debug("Calling API with sentences: %s, src_lang: %s, tgt_lang: %s", sentences, src_lang_code, tgt_lang_code)
61
+
62
+ # Call the API
63
+ try:
64
+ result = dwani.Translate.run_translate(sentences=sentences, src_lang=src_lang_code, tgt_lang=tgt_lang_code)
65
+ logger.debug("API response: %s", result)
66
+ return result
67
+ except dwani.exceptions.DhwaniAPIError as e:
68
+ logger.error("Dhwani API error: %s", str(e))
69
+ return {"error": f"API error: {str(e)}"}
70
+ except Exception as e:
71
+ logger.error("Unexpected error: %s", str(e))
72
+ return {"error": f"Unexpected error: {str(e)}"}
73
+
74
+ # Create Gradio interface
75
+ with gr.Blocks(title="Translation API Interface") as demo:
76
+ gr.Markdown("# Translation API Interface")
77
+ gr.Markdown("Enter one or more sentences (separated by commas) and select languages to translate.")
78
+
79
+ with gr.Row():
80
+ with gr.Column():
81
+ # Input components
82
+ sentences_input = gr.Textbox(
83
+ label="Sentences",
84
+ placeholder="Enter your sentence (e.g., Hello, Good morning)",
85
+ lines=3,
86
+ value="Hi"
87
+ )
88
+ src_lang_input = gr.Dropdown(
89
+ label="Source Language",
90
+ choices=LANGUAGES,
91
+ value="English"
92
+ )
93
+ tgt_lang_input = gr.Dropdown(
94
+ label="Target Language",
95
+ choices=LANGUAGES,
96
+ value="Kannada"
97
+ )
98
+
99
+ submit_btn = gr.Button("Translate")
100
+
101
+ with gr.Column():
102
+ # Output component
103
+ output = gr.JSON(label="Translation Response")
104
+
105
+ # Connect the button click to the API function
106
+ submit_btn.click(
107
+ fn=translate_api,
108
+ inputs=[sentences_input, src_lang_input, tgt_lang_input],
109
+ outputs=output
110
+ )
111
+
112
+ # Launch the interface
113
+ if __name__ == "__main__":
114
+ # Test API configuration
115
+ if not dwani.api_key or not dwani.api_base:
116
+ logger.error("API key or base URL not set. Please set DWANI_API_KEY and DWANI_API_BASE_URL environment variables.")
117
+ print("Error: Please set DWANI_API_KEY and DWANI_API_BASE_URL environment variables.")
118
+ else:
119
+ logger.debug("Starting Gradio interface...")
120
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ dwani