CEEMEESEEK / app.py
acecalisto3's picture
Update app.py
0934a74 verified
raw
history blame
3.58 kB
import gradio as gr
from agent import start_scraping, display_csv, generate_rss_feed, chat_interface
def validate_input(values):
"""Validate user input"""
if not values["storage_location"]:
raise ValueError("Storage location cannot be empty")
if not values["urls"]:
raise ValueError("URLs cannot be empty")
return values
def create_interface():
"""Create Gradio interface"""
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
message = gr.Textbox(label="Message")
system_message = gr.Textbox(value="You are a helpful assistant.", label="System message")
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
storage_location = gr.Textbox(value="scraped_data", label="Storage Location")
urls = gr.Textbox(label="URLs (comma separated)")
scrape_interval = gr.Slider(minimum=1, maximum=60, value=5, step=1, label="Scrape Interval (minutes)")
content_type = gr.Radio(choices=["text", "media", "both"], value="text", label="Content Type")
start_button = gr.Button("Start Scraping")
csv_output = gr.Textbox(label="CSV Output", interactive=False)
with gr.Column():
chat_history = gr.Chatbot(label="Chat History")
response_box = gr.Textbox(label="Response")
def start_scraping_callback(values):
"""Start scraping callback"""
try:
validated_values = validate_input(values)
start_scraping(validated_values["storage_location"], validated_values["urls"], validated_values["scrape_interval"], validated_values["content_type"])
display_csv(validated_values["storage_location"])
except Exception as e:
print(f"Error: {str(e)}")
start_button.click(start_scraping_callback, inputs=[storage_location, urls, scrape_interval, content_type], outputs=csv_output)
def chat_interface_callback(values):
"""Chat interface callback"""
try:
chat_interface(values["message"], chat_history, system_message, max_tokens, temperature, top_p, storage_location, urls, scrape_interval, content_type)
except Exception as e:
print(f"Error: {str(e)}")
message.submit(chat_interface_callback, inputs=[message, chat_history, system_message, max_tokens, temperature, top_p, storage_location, urls, scrape_interval, content_type], outputs=[chat_history, response_box])
# Add a button to display the RSS feed for a selected URL
with gr.Row():
selected_url = gr.Textbox(label="Select URL for RSS Feed")
rss_button = gr.Button("Generate RSS Feed")
rss_output = gr.Textbox(label="RSS Feed Output", interactive=False)
def generate_rss_feed_callback(values):
"""Generate RSS feed callback"""
try:
generate_rss_feed(values["selected_url"])
except Exception as e:
print(f"Error: {str(e)}")
rss_button.click(generate_rss_feed_callback, inputs=[selected_url], outputs=rss_output)
return demo
if __name__ == "__main__":
demo = create_interface()
demo.launch()