Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -93,32 +93,68 @@ editor_agent = Agent(
|
|
93 |
model=model
|
94 |
)
|
95 |
|
96 |
-
#
|
97 |
-
|
98 |
try:
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
101 |
except Exception as e:
|
102 |
-
return f"Error
|
103 |
|
104 |
# Workflow function for Gradio
|
105 |
def fetch_and_edit_news(topic, language, search_date):
|
106 |
try:
|
107 |
-
#
|
108 |
-
loop = asyncio.
|
109 |
-
|
110 |
-
# Step 1: Run the news agent
|
111 |
-
news_prompt = f"Get me the news about {topic} in {language} for date {search_date}"
|
112 |
-
raw_news = loop.run_until_complete(run_agent_async(news_agent, news_prompt))
|
113 |
-
|
114 |
-
if raw_news.startswith("Error"):
|
115 |
-
return raw_news
|
116 |
-
|
117 |
-
# Step 2: Pass news to editor for final review
|
118 |
-
editor_prompt = f"Please edit the following news in {language} language. Maintain the original language: \n\n{raw_news}"
|
119 |
-
edited_news = loop.run_until_complete(run_agent_async(editor_agent, editor_prompt))
|
120 |
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
except Exception as e:
|
123 |
return f"Error: {str(e)}\n\nPlease check your OpenAI API key and ensure it has been set correctly in the repository secrets."
|
124 |
|
|
|
93 |
model=model
|
94 |
)
|
95 |
|
96 |
+
# Fallback to direct OpenAI API if agents don't work
|
97 |
+
def generate_with_openai(prompt):
|
98 |
try:
|
99 |
+
response = client.chat.completions.create(
|
100 |
+
model="gpt-4o-mini",
|
101 |
+
messages=[{"role": "user", "content": prompt}],
|
102 |
+
temperature=0.7,
|
103 |
+
max_tokens=2000
|
104 |
+
)
|
105 |
+
return response.choices[0].message.content
|
106 |
except Exception as e:
|
107 |
+
return f"Error with OpenAI API: {str(e)}"
|
108 |
|
109 |
# Workflow function for Gradio
|
110 |
def fetch_and_edit_news(topic, language, search_date):
|
111 |
try:
|
112 |
+
# Initialize new event loop
|
113 |
+
loop = asyncio.new_event_loop()
|
114 |
+
asyncio.set_event_loop(loop)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
+
# Try to use Runner.run_sync with the proper event loop context
|
117 |
+
try:
|
118 |
+
# Step 1: Run the news agent to get news
|
119 |
+
news_prompt = f"Get me the news about {topic} in {language} for date {search_date}"
|
120 |
+
news_result = Runner.run_sync(news_agent, news_prompt)
|
121 |
+
raw_news = news_result.final_output
|
122 |
+
|
123 |
+
# Step 2: Run the editor agent to edit the news
|
124 |
+
editor_prompt = f"Please edit the following news in {language} language. Maintain the original language: \n\n{raw_news}"
|
125 |
+
editor_result = Runner.run_sync(editor_agent, editor_prompt)
|
126 |
+
edited_news = editor_result.final_output
|
127 |
+
|
128 |
+
return edited_news
|
129 |
+
|
130 |
+
except Exception as agent_error:
|
131 |
+
print(f"Agent error: {agent_error}")
|
132 |
+
|
133 |
+
# Fallback to direct use of DuckDuckGo and OpenAI if agents fail
|
134 |
+
try:
|
135 |
+
print("Falling back to direct search and API calls...")
|
136 |
+
# Get news directly
|
137 |
+
raw_news = get_news_articles(topic, language, search_date)
|
138 |
+
|
139 |
+
if raw_news.startswith("Error") or raw_news.startswith("Could not find"):
|
140 |
+
return raw_news
|
141 |
+
|
142 |
+
# Format prompt for OpenAI
|
143 |
+
editor_prompt = f"""
|
144 |
+
Please edit and reformat the following news into a cohesive, publication-ready format.
|
145 |
+
Maintain the original language ({language}).
|
146 |
+
Each news story should be in a separate section with a clear headline:
|
147 |
+
|
148 |
+
{raw_news}
|
149 |
+
"""
|
150 |
+
|
151 |
+
# Call OpenAI directly
|
152 |
+
edited_news = generate_with_openai(editor_prompt)
|
153 |
+
return edited_news
|
154 |
+
|
155 |
+
except Exception as fallback_error:
|
156 |
+
return f"Both agent-based and fallback approaches failed.\nError details: {str(fallback_error)}"
|
157 |
+
|
158 |
except Exception as e:
|
159 |
return f"Error: {str(e)}\n\nPlease check your OpenAI API key and ensure it has been set correctly in the repository secrets."
|
160 |
|