Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from shared import ResearchState | |
| from graph_article.graph_article import article_graph | |
| from graph_web.graph_web import web_graph | |
| from pydantic import ValidationError | |
| import os | |
| CREDITS_EXCEEDED_MSG = ( | |
| "You have exceeded your monthly included credits for Inference Providers. " | |
| "Subscribe to PRO to get 20x more monthly included credits." | |
| ) | |
| def generate_abstract(title, category): | |
| if not title or not category: | |
| return "Please enter both title and category." | |
| try: | |
| init_state = ResearchState(input=title, category=category) | |
| final_state = article_graph.invoke(init_state) | |
| if final_state.get("final_abstract"): | |
| return final_state["final_abstract"] | |
| else: | |
| return "No abstract was accepted by the critic." | |
| except Exception as e: | |
| err_str = str(e) | |
| if CREDITS_EXCEEDED_MSG in err_str: | |
| return CREDITS_EXCEEDED_MSG | |
| return f"Error: {err_str}" | |
| def summarize_webpage(url): | |
| if not url: | |
| return "Please enter a URL." | |
| try: | |
| init_state = ResearchState(url=url) | |
| final_state = web_graph.invoke(init_state) | |
| return final_state.get("summary", "No summary available.") | |
| except ValidationError as ve: | |
| return f"Invalid URL: {ve}" | |
| except Exception as e: | |
| err_str = str(e) | |
| if CREDITS_EXCEEDED_MSG in err_str: | |
| return CREDITS_EXCEEDED_MSG | |
| return f"Error: {err_str}" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Agentic Research Abstract Generator and Web Content Summariser Agent With LangGraph") | |
| with gr.Tab("Generate Research Abstract"): | |
| title_input = gr.Textbox(label="Research Title", lines=1) | |
| category_input = gr.Textbox(label="Category", lines=1) | |
| abstract_output = gr.Textbox(label="Final Abstract", lines=10) | |
| generate_btn = gr.Button("Generate Abstract") | |
| generate_btn.click( | |
| fn=generate_abstract, | |
| inputs=[title_input, category_input], | |
| outputs=abstract_output, | |
| ) | |
| with gr.Tab("Summarize Webpage"): | |
| url_input = gr.Textbox(label="URL to Summarize", lines=1) | |
| summary_output = gr.Textbox(label="Summary", lines=10) | |
| summarize_btn = gr.Button("Summarize") | |
| summarize_btn.click( | |
| fn=summarize_webpage, | |
| inputs=[url_input], | |
| outputs=summary_output, | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(pwa=True) |