duroodia commited on
Commit
2d6d0bb
·
verified ·
1 Parent(s): b43fd81

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ import gradio as gr
4
+ from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel
5
+ from agents.run import RunConfig
6
+ import asyncio
7
+
8
+ # Load environment variables from .env file
9
+ load_dotenv()
10
+
11
+ # Retrieve the Gemini API key from environment variables
12
+ gemini_api_key = os.getenv("GEMINI_API_KEY")
13
+
14
+ # Check if the API key is present; if not, raise an error
15
+ if not gemini_api_key:
16
+ raise ValueError("GEMINI_API_KEY is not set. Please ensure it is defined in your .env file.")
17
+
18
+ # Configure the AsyncOpenAI client for Gemini API
19
+ external_client = AsyncOpenAI(
20
+ api_key=gemini_api_key,
21
+ base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
22
+ )
23
+
24
+ # Define the model
25
+ model = OpenAIChatCompletionsModel(
26
+ model="gemini-2.0-flash",
27
+ openai_client=external_client
28
+ )
29
+
30
+ # Configure the run settings
31
+ config = RunConfig(
32
+ model=model,
33
+ model_provider=external_client,
34
+ tracing_disabled=True
35
+ )
36
+
37
+ # Define the translation function
38
+ async def translate_urdu_to_english(urdu_text):
39
+ agent = Agent(
40
+ name="Translator",
41
+ instructions="You are a translator, always translate Urdu sentences into English Language.",
42
+ model=model
43
+ )
44
+ result = await Runner.run(agent, urdu_text, run_config=config)
45
+ return result.final_output
46
+
47
+ # Gradio interface function
48
+ def gradio_translate(urdu_input):
49
+ # Run the async translation function within Gradio
50
+ return asyncio.run(translate_urdu_to_english(urdu_input))
51
+
52
+ # Create Gradio interface
53
+ iface = gr.Interface(
54
+ fn=gradio_translate,
55
+ inputs=gr.Textbox(label="Urdu Input", placeholder="Enter Urdu sentence here..."),
56
+ outputs=gr.Textbox(label="English Translation"),
57
+ title="Urdu to English Translator",
58
+ description="Enter an Urdu sentence to get its English translation using the Gemini API."
59
+ )
60
+
61
+ # Launch the Gradio app
62
+ if __name__ == "__main__":
63
+ iface.launch()