GayathriKavitha commited on
Commit
83b48b4
Β·
verified Β·
1 Parent(s): f6c0612
Files changed (1) hide show
  1. app.py +168 -56
app.py CHANGED
@@ -1,69 +1,181 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
 
19
- messages = [{"role": "system", "content": system_message}]
 
20
 
21
- messages.extend(history)
 
22
 
23
- messages.append({"role": "user", "content": message})
24
 
25
- response = ""
 
 
 
 
 
 
 
26
 
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
 
39
- response += token
40
- yield response
41
 
 
42
 
 
 
43
  """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
- with gr.Blocks() as demo:
63
- with gr.Sidebar():
64
- gr.LoginButton()
65
- chatbot.render()
66
-
67
-
68
- if __name__ == "__main__":
69
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
 
5
+ # ----------------------------------------
6
+ # Supported Languages
7
+ # ----------------------------------------
8
+ languages = [
9
+ "English",
10
+ "Hindi",
11
+ "Tamil",
12
+ "Telugu",
13
+ "Kannada",
14
+ "Malayalam",
15
+ "Bengali",
16
+ "Marathi",
17
+ "Gujarati",
18
+ "Punjabi",
19
+ "Urdu"
20
+ ]
21
 
22
+ # ----------------------------------------
23
+ # Translation Function
24
+ # ----------------------------------------
25
+ def translate_text(hf_token, source_lang, target_lang, input_text):
 
 
 
 
 
 
 
 
 
26
 
27
+ if not hf_token.strip():
28
+ return "Please enter Hugging Face Access Token."
29
 
30
+ if not input_text.strip():
31
+ return "Please enter text."
32
 
33
+ try:
34
 
35
+ # Load IBM Granite model
36
+ pipe = pipeline(
37
+ "text-generation",
38
+ model="ibm-granite/granite-3.3-2b-base",
39
+ token=hf_token,
40
+ device_map="auto",
41
+ torch_dtype=torch.float16
42
+ )
43
 
44
+ # Translation Prompt
45
+ prompt = f"""
46
+ You are an expert multilingual translator.
 
 
 
 
 
 
 
 
47
 
48
+ Translate the below text from {source_lang} to {target_lang}.
 
49
 
50
+ Only provide translated text.
51
 
52
+ Text:
53
+ {input_text}
54
  """
55
+
56
+ # Generate Translation
57
+ result = pipe(
58
+ prompt,
59
+ max_new_tokens=200,
60
+ temperature=0.2
61
+ )
62
+
63
+ translated_text = result[0]["generated_text"]
64
+
65
+ # Remove original prompt if generated
66
+ translated_text = translated_text.replace(prompt, "").strip()
67
+
68
+ return translated_text
69
+
70
+ except Exception as e:
71
+ return f"Error: {str(e)}"
72
+
73
+
74
+ # ----------------------------------------
75
+ # Professional UI Styling
76
+ # ----------------------------------------
77
+ custom_css = """
78
+ body {
79
+ background: linear-gradient(to right, #0f2027, #203a43, #2c5364);
80
+ }
81
+
82
+ .gradio-container {
83
+ font-family: 'Segoe UI', sans-serif;
84
+ }
85
+
86
+ .main-title {
87
+ text-align: center;
88
+ font-size: 42px;
89
+ font-weight: bold;
90
+ color: white;
91
+ }
92
+
93
+ .sub-title {
94
+ text-align: center;
95
+ color: #dfe6e9;
96
+ font-size: 18px;
97
+ margin-bottom: 20px;
98
+ }
99
+
100
+ textarea {
101
+ border-radius: 12px !important;
102
+ }
103
+
104
+ footer {
105
+ visibility: hidden;
106
+ }
107
  """
108
+
109
+ # ----------------------------------------
110
+ # Gradio Interface
111
+ # ----------------------------------------
112
+ with gr.Blocks(
113
+ theme=gr.themes.Soft(),
114
+ css=custom_css
115
+ ) as demo:
116
+
117
+ gr.Markdown(
118
+ """
119
+ <div class="main-title">
120
+ 🌍 AI Multilingual Translator
121
+ </div>
122
+
123
+ <div class="sub-title">
124
+ IBM Granite + Hugging Face + Gradio
125
+ </div>
126
+ """
127
+ )
128
+
129
+ with gr.Row():
130
+
131
+ with gr.Column(scale=1):
132
+
133
+ hf_token = gr.Textbox(
134
+ label="πŸ”‘ Hugging Face Access Token",
135
+ placeholder="Paste your HF token here",
136
+ type="password"
137
+ )
138
+
139
+ source_lang = gr.Dropdown(
140
+ choices=languages,
141
+ value="English",
142
+ label="🌐 Source Language"
143
+ )
144
+
145
+ target_lang = gr.Dropdown(
146
+ choices=languages,
147
+ value="Hindi",
148
+ label="🎯 Target Language"
149
+ )
150
+
151
+ with gr.Column(scale=2):
152
+
153
+ input_text = gr.Textbox(
154
+ label="πŸ“ Enter Text",
155
+ lines=8,
156
+ placeholder="Type text to translate..."
157
+ )
158
+
159
+ output_text = gr.Textbox(
160
+ label="βœ… Translated Output",
161
+ lines=8
162
+ )
163
+
164
+ translate_btn = gr.Button(
165
+ "πŸš€ Translate",
166
+ variant="primary"
167
+ )
168
+
169
+ translate_btn.click(
170
+ fn=translate_text,
171
+ inputs=[
172
+ hf_token,
173
+ source_lang,
174
+ target_lang,
175
+ input_text
176
+ ],
177
+ outputs=output_text
178
+ )
179
+
180
+ # Launch App
181
+ demo.launch(share=True)