Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1 +1,120 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from scrapegraphai.graphs import SmartScraperGraph
|
4 |
+
from scrapegraphai.utils import prettify_exec_info
|
5 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
6 |
+
from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
|
7 |
+
import gradio as gr
|
8 |
+
import subprocess
|
9 |
+
import json
|
10 |
+
import re
|
11 |
+
|
12 |
+
# Ensure Playwright installs required browsers and dependencies
|
13 |
+
subprocess.run(["playwright", "install"])
|
14 |
+
#subprocess.run(["playwright", "install-deps"])
|
15 |
+
|
16 |
+
# Load environment variables
|
17 |
+
load_dotenv()
|
18 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
|
19 |
+
|
20 |
+
# Initialize the model instances
|
21 |
+
#repo_id = "mistralai/Mistral-7B-Instruct-v0.2"
|
22 |
+
repo_id = "Qwen/Qwen2.5-72B-Instruct"
|
23 |
+
|
24 |
+
llm_model_instance = HuggingFaceEndpoint(
|
25 |
+
repo_id=repo_id, max_length=128, temperature=0.5, token=HUGGINGFACEHUB_API_TOKEN
|
26 |
+
)
|
27 |
+
|
28 |
+
embedder_model_instance = HuggingFaceInferenceAPIEmbeddings(
|
29 |
+
api_key=HUGGINGFACEHUB_API_TOKEN, model_name="sentence-transformers/all-MiniLM-l6-v2"
|
30 |
+
)
|
31 |
+
|
32 |
+
graph_config = {
|
33 |
+
"llm": {
|
34 |
+
"model_instance": llm_model_instance,
|
35 |
+
"model_tokens": 100000,
|
36 |
+
},
|
37 |
+
"embeddings": {"model_instance": embedder_model_instance}
|
38 |
+
}
|
39 |
+
#######
|
40 |
+
def clean_json_string(json_str):
|
41 |
+
"""
|
42 |
+
Removes any comments or prefixes before the actual JSON content.
|
43 |
+
Returns the cleaned JSON string.
|
44 |
+
"""
|
45 |
+
# Find the first occurrence of '{'
|
46 |
+
json_start = json_str.find('{')
|
47 |
+
if json_start == -1:
|
48 |
+
# If no '{' is found, try with '[' for arrays
|
49 |
+
json_start = json_str.find('[')
|
50 |
+
if json_start == -1:
|
51 |
+
return json_str # Return original if no JSON markers found
|
52 |
+
|
53 |
+
# Extract everything from the first JSON marker
|
54 |
+
cleaned_str = json_str[json_start:]
|
55 |
+
|
56 |
+
# Verify it's valid JSON
|
57 |
+
try:
|
58 |
+
json.loads(cleaned_str)
|
59 |
+
return cleaned_str
|
60 |
+
except json.JSONDecodeError:
|
61 |
+
return json_str # Return original if cleaning results in invalid JSON
|
62 |
+
|
63 |
+
def scrape_and_summarize(prompt, source):
|
64 |
+
smart_scraper_graph = SmartScraperGraph(
|
65 |
+
prompt=prompt,
|
66 |
+
source=source,
|
67 |
+
config=graph_config
|
68 |
+
)
|
69 |
+
result = smart_scraper_graph.run()
|
70 |
+
|
71 |
+
# Clean the result if it's a string
|
72 |
+
if isinstance(result, str):
|
73 |
+
result = clean_json_string(result)
|
74 |
+
|
75 |
+
exec_info = smart_scraper_graph.get_execution_info()
|
76 |
+
return result, prettify_exec_info(exec_info)
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
#######
|
81 |
+
# def scrape_and_summarize(prompt, source):
|
82 |
+
# smart_scraper_graph = SmartScraperGraph(
|
83 |
+
# prompt=prompt,
|
84 |
+
# source=source,
|
85 |
+
# config=graph_config
|
86 |
+
# )
|
87 |
+
# result = smart_scraper_graph.run()
|
88 |
+
# exec_info = smart_scraper_graph.get_execution_info()
|
89 |
+
# return result, prettify_exec_info(exec_info)
|
90 |
+
|
91 |
+
# Gradio interface
|
92 |
+
with gr.Blocks() as demo:
|
93 |
+
gr.Markdown("# Scrape websites, no-code version")
|
94 |
+
gr.Markdown("""
|
95 |
+
Easily scrape and summarize web content using advanced AI models on the Hugging Face Hub without writing any code. Input your desired prompt and source URL to get started.
|
96 |
+
This is a no-code version of the excellent library [ScrapeGraphAI](https://github.com/VinciGit00/Scrapegraph-ai).
|
97 |
+
It's a basic demo and a work in progress. Please contribute to it to make it more useful!
|
98 |
+
*Note: You might need to add "Output only the results; do not add any comments or include 'JSON OUTPUT' or similar phrases" in your prompt to ensure the LLM only provides the result.*
|
99 |
+
""")
|
100 |
+
with gr.Row():
|
101 |
+
with gr.Column():
|
102 |
+
|
103 |
+
model_dropdown = gr.Textbox(label="Model", value="Qwen/Qwen2.5-72B-Instruct")
|
104 |
+
prompt_input = gr.Textbox(label="Prompt", value="List all the press releases with their headlines and urls. Output only the results; do not add any comments or include 'JSON OUTPUT' or similar phrases.")
|
105 |
+
source_input = gr.Textbox(label="Source URL", value="https://www.whitehouse.gov/")
|
106 |
+
scrape_button = gr.Button("Scrape and Summarize")
|
107 |
+
|
108 |
+
with gr.Column():
|
109 |
+
result_output = gr.JSON(label="Result")
|
110 |
+
exec_info_output = gr.Textbox(label="Execution Info")
|
111 |
+
|
112 |
+
scrape_button.click(
|
113 |
+
scrape_and_summarize,
|
114 |
+
inputs=[prompt_input, source_input],
|
115 |
+
outputs=[result_output, exec_info_output]
|
116 |
+
)
|
117 |
+
|
118 |
+
# Launch the Gradio app
|
119 |
+
if __name__ == "__main__":
|
120 |
+
demo.launch()
|