Upload 3 files
Browse files- README.md +31 -14
- app.py +147 -0
- requirements.txt +6 -0
README.md
CHANGED
@@ -1,14 +1,31 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Multilingual AI News Generator
|
2 |
+
|
3 |
+
This application uses LLM agents to fetch and edit news articles on any topic in multiple languages. It combines DuckDuckGo search with AI text generation to create ready-to-publish news articles in the language of your choice.
|
4 |
+
|
5 |
+
## How it works
|
6 |
+
|
7 |
+
1. Enter a topic in the textbox
|
8 |
+
2. Select your preferred language from the dropdown (19 languages supported)
|
9 |
+
3. Click "Generate News Article"
|
10 |
+
4. The app will:
|
11 |
+
- Search for recent news on your topic in the selected language
|
12 |
+
- Compile the search results
|
13 |
+
- Use an AI editor to rewrite the articles into a cohesive, publication-ready format in the selected language
|
14 |
+
|
15 |
+
## Technical Details
|
16 |
+
|
17 |
+
This application uses:
|
18 |
+
- DuckDuckGo search API for fetching news in multiple languages
|
19 |
+
- OpenAI's GPT-4o-mini model for multilingual text generation and editing
|
20 |
+
- Agents framework for orchestrating multi-step AI workflows
|
21 |
+
- Gradio for the user interface
|
22 |
+
- Support for 19 languages including English, Hindi, Kannada, Spanish, and more
|
23 |
+
|
24 |
+
## Setup
|
25 |
+
|
26 |
+
If you're running this locally, you'll need to set up the following environment variables:
|
27 |
+
- `OPENAI_API_KEY`: Your OpenAI API key
|
28 |
+
|
29 |
+
## Credits
|
30 |
+
|
31 |
+
Built with ❤️ using Gradio and Hugging Face
|
app.py
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from duckduckgo_search import DDGS
|
3 |
+
from datetime import datetime
|
4 |
+
import os
|
5 |
+
from openai import OpenAI # Using standard OpenAI client
|
6 |
+
from agents import Agent, Runner, function_tool # Assuming agents package is installed
|
7 |
+
|
8 |
+
# Set up environment variables
|
9 |
+
# For Hugging Face Spaces, set these in the Settings > Repository secrets
|
10 |
+
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") # You'll need to add this to HF Spaces secrets
|
11 |
+
|
12 |
+
# Get current date for default value
|
13 |
+
default_date = datetime.now().strftime("%Y-%m-%d")
|
14 |
+
|
15 |
+
# Configure OpenAI client to use HuggingFace or OpenAI API
|
16 |
+
client = OpenAI(
|
17 |
+
api_key=OPENAI_API_KEY,
|
18 |
+
# If using OpenAI directly
|
19 |
+
# If using a different API endpoint (e.g., HF Inference API), uncomment and adjust:
|
20 |
+
# base_url="https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-70b-instruct"
|
21 |
+
)
|
22 |
+
|
23 |
+
# Define the model - assuming the agents library supports standard OpenAI client
|
24 |
+
from agents import OpenAIChatCompletionsModel # Adjust import if needed
|
25 |
+
|
26 |
+
model = OpenAIChatCompletionsModel(
|
27 |
+
model="gpt-4o-mini", # Using GPT-4o-mini for better performance at a lower cost
|
28 |
+
openai_client=client
|
29 |
+
)
|
30 |
+
|
31 |
+
# News search tool
|
32 |
+
@function_tool
|
33 |
+
def get_news_articles(topic, language="English", search_date=None):
|
34 |
+
# Use provided date or default to current date
|
35 |
+
if not search_date:
|
36 |
+
search_date = datetime.now().strftime("%Y-%m")
|
37 |
+
else:
|
38 |
+
# Convert from date picker format (YYYY-MM-DD) to YYYY-MM format
|
39 |
+
search_date = search_date[:7] # Just get YYYY-MM portion
|
40 |
+
|
41 |
+
print(f"Running DuckDuckGo news search for {topic} in {language} for date {search_date}...")
|
42 |
+
|
43 |
+
# Map common languages to their search keywords
|
44 |
+
language_keywords = {
|
45 |
+
"English": "", # Default, no special keyword needed
|
46 |
+
"Hindi": "हिंदी",
|
47 |
+
"Spanish": "español",
|
48 |
+
"French": "français",
|
49 |
+
"German": "deutsch",
|
50 |
+
"Japanese": "日本語",
|
51 |
+
"Chinese": "中文",
|
52 |
+
"Russian": "русский",
|
53 |
+
"Arabic": "العربية",
|
54 |
+
"Portuguese": "português",
|
55 |
+
"Italian": "italiano",
|
56 |
+
"Dutch": "nederlands",
|
57 |
+
"Korean": "한국어",
|
58 |
+
"Turkish": "türkçe",
|
59 |
+
"Kannada": "ಕನ್ನಡ",
|
60 |
+
"Tamil": "தமிழ்",
|
61 |
+
"Telugu": "తెలుగు",
|
62 |
+
"Bengali": "বাংলা",
|
63 |
+
"Marathi": "मराठी"
|
64 |
+
}
|
65 |
+
|
66 |
+
# Get language keyword if available
|
67 |
+
lang_keyword = language_keywords.get(language, language)
|
68 |
+
|
69 |
+
# Add language to search query if it's not English
|
70 |
+
search_query = f"{topic} {lang_keyword} {search_date}" if language != "English" else f"{topic} {search_date}"
|
71 |
+
|
72 |
+
# DuckDuckGo search
|
73 |
+
ddg_api = DDGS()
|
74 |
+
results = ddg_api.text(search_query, max_results=5)
|
75 |
+
if results:
|
76 |
+
news_results = "\n\n".join([f"Title: {result['title']}\nURL: {result['href']}\nDescription: {result['body']}" for result in results])
|
77 |
+
return news_results
|
78 |
+
else:
|
79 |
+
return f"Could not find news results for {topic} in {language} for {search_date}."
|
80 |
+
|
81 |
+
# Create agents
|
82 |
+
news_agent = Agent(
|
83 |
+
name="News Agent",
|
84 |
+
instructions="You provide the latest news articles for a given topic using DuckDuckGo search. You can search for news in different languages when specified.",
|
85 |
+
tools=[get_news_articles],
|
86 |
+
model=model
|
87 |
+
)
|
88 |
+
|
89 |
+
editor_agent = Agent(
|
90 |
+
name="Editor Assistant",
|
91 |
+
instructions="Rewrite and give me a news article ready for publishing. Each news story should be in a separate section. Maintain the original language of the news stories. If the content is in a language other than English, edit and format in that same language.",
|
92 |
+
model=model
|
93 |
+
)
|
94 |
+
|
95 |
+
# Workflow function for Gradio
|
96 |
+
def fetch_and_edit_news(topic, language, search_date):
|
97 |
+
# Step 1: Run the news agent
|
98 |
+
news_result = Runner.run_sync(
|
99 |
+
news_agent,
|
100 |
+
f"Get me the news about {topic} in {language} for date {search_date}")
|
101 |
+
|
102 |
+
raw_news = news_result.final_output
|
103 |
+
|
104 |
+
# Step 2: Pass news to editor for final review
|
105 |
+
editor_news_response = Runner.run_sync(
|
106 |
+
editor_agent,
|
107 |
+
f"Please edit the following news in {language} language. Maintain the original language: \n\n{raw_news}")
|
108 |
+
|
109 |
+
edited_news = editor_news_response.final_output
|
110 |
+
|
111 |
+
return edited_news
|
112 |
+
|
113 |
+
# Create Gradio interface
|
114 |
+
with gr.Blocks(title="Multilingual AI News Generator") as demo:
|
115 |
+
gr.Markdown("# Multilingual AI News Generator")
|
116 |
+
gr.Markdown("Enter a topic, select a language, and choose a date to receive curated and edited news articles")
|
117 |
+
|
118 |
+
with gr.Row():
|
119 |
+
topic_input = gr.Textbox(label="News Topic", placeholder="Enter a topic (e.g., AI, Climate Change, Sports)")
|
120 |
+
language_dropdown = gr.Dropdown(
|
121 |
+
choices=[
|
122 |
+
"English", "Hindi", "Spanish", "French", "German",
|
123 |
+
"Japanese", "Chinese", "Russian", "Arabic", "Portuguese",
|
124 |
+
"Italian", "Dutch", "Korean", "Turkish", "Kannada",
|
125 |
+
"Tamil", "Telugu", "Bengali", "Marathi"
|
126 |
+
],
|
127 |
+
label="Language",
|
128 |
+
value="English"
|
129 |
+
)
|
130 |
+
date_picker = gr.Textbox(
|
131 |
+
label="Search Date",
|
132 |
+
placeholder="YYYY-MM-DD",
|
133 |
+
value=default_date
|
134 |
+
)
|
135 |
+
submit_btn = gr.Button("Generate News Article")
|
136 |
+
|
137 |
+
output_box = gr.Textbox(label="Generated News Article", lines=20)
|
138 |
+
|
139 |
+
submit_btn.click(
|
140 |
+
fn=fetch_and_edit_news,
|
141 |
+
inputs=[topic_input, language_dropdown, date_picker],
|
142 |
+
outputs=output_box
|
143 |
+
)
|
144 |
+
|
145 |
+
# Launch the app
|
146 |
+
if __name__ == "__main__":
|
147 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio>=4.0.0
|
2 |
+
duckduckgo-search>=1.5.0
|
3 |
+
openai>=1.0.0
|
4 |
+
openai-agents>=0.1.0
|
5 |
+
datetime
|
6 |
+
python-dotenv>=1.0.0
|