File size: 2,563 Bytes
e6583bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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)