TRendi / app.py
typesdigital's picture
Create app.py
b53cb99 verified
import gradio as gr
import pymongo
from bson import ObjectId
from datetime import datetime
import bcrypt
import os
# MongoDB setup
client = pymongo.MongoClient(os.environ.get("MONGO_URI", "mongodb://localhost:27017/"))
db = client.aitrendpulse
def register_user(username, password):
users = db.users
existing_user = users.find_one({"username": username})
if existing_user:
return "Username already exists"
hashed = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
user_id = users.insert_one({
'username': username,
'password': hashed,
'created_at': datetime.utcnow()
}).inserted_id
return f"User registered successfully. User ID: {str(user_id)}"
def login_user(username, password):
users = db.users
user = users.find_one({'username': username})
if user and bcrypt.checkpw(password.encode('utf-8'), user['password']):
return f"Login successful. User ID: {str(user['_id'])}"
else:
return "Invalid username or password"
def get_trends():
trends = db.trends.find().sort('created_at', -1).limit(10)
return "\n".join([f"Title: {trend['title']}, Category: {trend['category']}" for trend in trends])
def add_trend(title, description, category):
trends = db.trends
trend_id = trends.insert_one({
'title': title,
'description': description,
'category': category,
'created_at': datetime.utcnow()
}).inserted_id
return f"Trend added successfully. Trend ID: {str(trend_id)}"
def get_preferences(user_id):
preferences = db.preferences.find_one({'user_id': ObjectId(user_id)})
if preferences:
return f"Categories: {', '.join(preferences['categories'])}"
else:
return "Preferences not found"
def update_preferences(user_id, categories):
preferences = db.preferences
categories_list = [cat.strip() for cat in categories.split(',')]
result = preferences.update_one(
{'user_id': ObjectId(user_id)},
{'$set': {
'categories': categories_list,
'updated_at': datetime.utcnow()
}},
upsert=True
)
if result.modified_count > 0 or result.upserted_id:
return "Preferences updated successfully"
else:
return "No changes made to preferences"
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# AITrendPulse")
with gr.Tab("User Management"):
with gr.Group():
gr.Markdown("## Register User")
register_username = gr.Textbox(label="Username")
register_password = gr.Textbox(label="Password", type="password")
register_button = gr.Button("Register")
register_output = gr.Textbox(label="Result")
register_button.click(register_user, inputs=[register_username, register_password], outputs=register_output)
with gr.Group():
gr.Markdown("## Login User")
login_username = gr.Textbox(label="Username")
login_password = gr.Textbox(label="Password", type="password")
login_button = gr.Button("Login")
login_output = gr.Textbox(label="Result")
login_button.click(login_user, inputs=[login_username, login_password], outputs=login_output)
with gr.Tab("Trend Management"):
with gr.Group():
gr.Markdown("## Get Trends")
get_trends_button = gr.Button("Get Trends")
trends_output = gr.Textbox(label="Trends")
get_trends_button.click(get_trends, inputs=[], outputs=trends_output)
with gr.Group():
gr.Markdown("## Add Trend")
trend_title = gr.Textbox(label="Title")
trend_description = gr.Textbox(label="Description")
trend_category = gr.Textbox(label="Category")
add_trend_button = gr.Button("Add Trend")
add_trend_output = gr.Textbox(label="Result")
add_trend_button.click(add_trend, inputs=[trend_title, trend_description, trend_category], outputs=add_trend_output)
with gr.Tab("User Preferences"):
with gr.Group():
gr.Markdown("## Get Preferences")
get_pref_user_id = gr.Textbox(label="User ID")
get_pref_button = gr.Button("Get Preferences")
get_pref_output = gr.Textbox(label="Preferences")
get_pref_button.click(get_preferences, inputs=[get_pref_user_id], outputs=get_pref_output)
with gr.Group():
gr.Markdown("## Update Preferences")
update_pref_user_id = gr.Textbox(label="User ID")
update_pref_categories = gr.Textbox(label="Categories (comma-separated)")
update_pref_button = gr.Button("Update Preferences")
update_pref_output = gr.Textbox(label="Result")
update_pref_button.click(update_preferences, inputs=[update_pref_user_id, update_pref_categories], outputs=update_pref_output)
demo.launch()