Spaces:
Configuration error
Configuration error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from flask import Flask, jsonify, request
|
3 |
+
from flask_pymongo import PyMongo
|
4 |
+
from bson import ObjectId
|
5 |
+
from datetime import datetime
|
6 |
+
import bcrypt
|
7 |
+
import os
|
8 |
+
|
9 |
+
app = Flask(__name__)
|
10 |
+
app.config["MONGO_URI"] = os.environ.get("MONGO_URI", "mongodb://localhost:27017/aitrendpulse")
|
11 |
+
mongo = PyMongo(app)
|
12 |
+
|
13 |
+
# User Management
|
14 |
+
def register_user(username, password):
|
15 |
+
users = mongo.db.users
|
16 |
+
hashed = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
17 |
+
|
18 |
+
user_id = users.insert_one({
|
19 |
+
'username': username,
|
20 |
+
'password': hashed,
|
21 |
+
'created_at': datetime.utcnow()
|
22 |
+
}).inserted_id
|
23 |
+
|
24 |
+
return f"User registered successfully. User ID: {str(user_id)}"
|
25 |
+
|
26 |
+
def login_user(username, password):
|
27 |
+
users = mongo.db.users
|
28 |
+
user = users.find_one({'username': username})
|
29 |
+
|
30 |
+
if user and bcrypt.checkpw(password.encode('utf-8'), user['password']):
|
31 |
+
return f"Login successful. User ID: {str(user['_id'])}"
|
32 |
+
else:
|
33 |
+
return "Invalid username or password"
|
34 |
+
|
35 |
+
# Trend Management
|
36 |
+
def get_trends():
|
37 |
+
trends = mongo.db.trends.find().sort('created_at', -1).limit(10)
|
38 |
+
return "\n".join([f"Title: {trend['title']}, Category: {trend['category']}" for trend in trends])
|
39 |
+
|
40 |
+
def add_trend(title, description, category):
|
41 |
+
trends = mongo.db.trends
|
42 |
+
trend_id = trends.insert_one({
|
43 |
+
'title': title,
|
44 |
+
'description': description,
|
45 |
+
'category': category,
|
46 |
+
'created_at': datetime.utcnow()
|
47 |
+
}).inserted_id
|
48 |
+
|
49 |
+
return f"Trend added successfully. Trend ID: {str(trend_id)}"
|
50 |
+
|
51 |
+
# User Preferences
|
52 |
+
def get_preferences(user_id):
|
53 |
+
preferences = mongo.db.preferences.find_one({'user_id': ObjectId(user_id)})
|
54 |
+
if preferences:
|
55 |
+
return f"Categories: {', '.join(preferences['categories'])}"
|
56 |
+
else:
|
57 |
+
return "Preferences not found"
|
58 |
+
|
59 |
+
def update_preferences(user_id, categories):
|
60 |
+
preferences = mongo.db.preferences
|
61 |
+
categories_list = [cat.strip() for cat in categories.split(',')]
|
62 |
+
|
63 |
+
result = preferences.update_one(
|
64 |
+
{'user_id': ObjectId(user_id)},
|
65 |
+
{'$set': {
|
66 |
+
'categories': categories_list,
|
67 |
+
'updated_at': datetime.utcnow()
|
68 |
+
}},
|
69 |
+
upsert=True
|
70 |
+
)
|
71 |
+
|
72 |
+
if result.modified_count > 0 or result.upserted_id:
|
73 |
+
return "Preferences updated successfully"
|
74 |
+
else:
|
75 |
+
return "No changes made to preferences"
|
76 |
+
|
77 |
+
# Gradio Interface
|
78 |
+
def create_gradio_interface():
|
79 |
+
with gr.Blocks() as demo:
|
80 |
+
gr.Markdown("# AITrendPulse")
|
81 |
+
|
82 |
+
with gr.Tab("User Management"):
|
83 |
+
with gr.Group():
|
84 |
+
gr.Markdown("## Register User")
|
85 |
+
register_username = gr.Textbox(label="Username")
|
86 |
+
register_password = gr.Textbox(label="Password", type="password")
|
87 |
+
register_button = gr.Button("Register")
|
88 |
+
register_output = gr.Textbox(label="Result")
|
89 |
+
register_button.click(register_user, inputs=[register_username, register_password], outputs=register_output)
|
90 |
+
|
91 |
+
with gr.Group():
|
92 |
+
gr.Markdown("## Login User")
|
93 |
+
login_username = gr.Textbox(label="Username")
|
94 |
+
login_password = gr.Textbox(label="Password", type="password")
|
95 |
+
login_button = gr.Button("Login")
|
96 |
+
login_output = gr.Textbox(label="Result")
|
97 |
+
login_button.click(login_user, inputs=[login_username, login_password], outputs=login_output)
|
98 |
+
|
99 |
+
with gr.Tab("Trend Management"):
|
100 |
+
with gr.Group():
|
101 |
+
gr.Markdown("## Get Trends")
|
102 |
+
get_trends_button = gr.Button("Get Trends")
|
103 |
+
trends_output = gr.Textbox(label="Trends")
|
104 |
+
get_trends_button.click(get_trends, inputs=[], outputs=trends_output)
|
105 |
+
|
106 |
+
with gr.Group():
|
107 |
+
gr.Markdown("## Add Trend")
|
108 |
+
trend_title = gr.Textbox(label="Title")
|
109 |
+
trend_description = gr.Textbox(label="Description")
|
110 |
+
trend_category = gr.Textbox(label="Category")
|
111 |
+
add_trend_button = gr.Button("Add Trend")
|
112 |
+
add_trend_output = gr.Textbox(label="Result")
|
113 |
+
add_trend_button.click(add_trend, inputs=[trend_title, trend_description, trend_category], outputs=add_trend_output)
|
114 |
+
|
115 |
+
with gr.Tab("User Preferences"):
|
116 |
+
with gr.Group():
|
117 |
+
gr.Markdown("## Get Preferences")
|
118 |
+
get_pref_user_id = gr.Textbox(label="User ID")
|
119 |
+
get_pref_button = gr.Button("Get Preferences")
|
120 |
+
get_pref_output = gr.Textbox(label="Preferences")
|
121 |
+
get_pref_button.click(get_preferences, inputs=[get_pref_user_id], outputs=get_pref_output)
|
122 |
+
|
123 |
+
with gr.Group():
|
124 |
+
gr.Markdown("## Update Preferences")
|
125 |
+
update_pref_user_id = gr.Textbox(label="User ID")
|
126 |
+
update_pref_categories = gr.Textbox(label="Categories (comma-separated)")
|
127 |
+
update_pref_button = gr.Button("Update Preferences")
|
128 |
+
update_pref_output = gr.Textbox(label="Result")
|
129 |
+
update_pref_button.click(update_preferences, inputs=[update_pref_user_id, update_pref_categories], outputs=update_pref_output)
|
130 |
+
|
131 |
+
return demo
|
132 |
+
|
133 |
+
# Create and launch the Gradio interface
|
134 |
+
interface = create_gradio_interface()
|
135 |
+
interface.launch()
|