Spaces:
Build error
Build error
import os | |
from flask_sqlalchemy import SQLAlchemy | |
from langchain_groq import ChatGroq | |
from langchain.prompts import ChatPromptTemplate | |
from langchain_core.output_parsers import StrOutputParser | |
from gradio import Interface, Textbox, Dropdown, Markdown | |
from textwrap import dedent | |
import requests | |
import json | |
import nest_asyncio | |
# Function to fetch recent news articles | |
def fetch_recent_news(): | |
# Simulate fetching news articles from the dataset | |
articles = [] | |
with open('.gradio/flagged/dataset1.csv', 'r') as file: | |
for line in file: | |
articles.append(line.strip()) # Assuming each line is a news title | |
return articles | |
# Function to analyze news data for trends | |
def analyze_news_for_trends(): | |
news_titles = fetch_recent_news() | |
# Simple analysis: return the top news titles as trends | |
# Format the news titles into a Markdown string | |
trending_topics = "\n".join(f"- {title}" for title in news_titles[:5]) | |
return f"### Trending Topics:\n{trending_topics}" | |
# Apply nested asyncio | |
nest_asyncio.apply() | |
# Set the API key for Groq | |
os.environ["GROQ_API_KEY"] = "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" | |
# Helper function to create agents | |
def create_agent(system_prompt: str, model_name: str) -> ChatGroq: | |
prompt = ChatPromptTemplate.from_messages([ | |
("system", system_prompt), | |
("human", "{input}") | |
]) | |
llm = ChatGroq(model=model_name) | |
return prompt | llm | StrOutputParser() | |
# Combined function that handles all tasks | |
def handle_task(task_type, query): | |
if task_type == "Trends": | |
# Fetch and return trending topics based on recent news | |
return analyze_news_for_trends() | |
elif task_type == "Script Generation": | |
system_prompt = """ | |
You are a creative expert who writes scripts with the perfect formula for TikTok virality. | |
Generate a detailed, engaging script based on the query. | |
""" | |
agent = create_agent(system_prompt, model_name="llama3-8b-8192") | |
return agent.invoke({"input": query}) | |
elif task_type == "Hashtag Generation": | |
system_prompt = """ | |
You are skilled at generating hashtags and tags for social media platforms. | |
Based on the query, provide the following: | |
- 30 unique TikTok viral tags | |
- 50 most popular hashtags | |
- 50 trending hashtags | |
- 50 FYP-related tags | |
- 25 YouTube viral keyword tags | |
- A clickbait title with emojis | |
""" | |
agent = create_agent(system_prompt, model_name="llama3-8b-8192") | |
return agent.invoke({"input": query}) | |
# Function to simulate fetching TikTok profile data | |
def fetch_tiktok_profile_data(profile_url): | |
# Make an actual API call to fetch TikTok profile data | |
response = requests.get(f"https://api.tiktok.com/profile?url={profile_url}") # Check if this URL is correct | |
print(f"Fetching profile data from: {response.url}") # Log the URL being fetched | |
print(f"Response Status Code: {response.status_code}") # Log the response status code | |
if response.status_code == 200: | |
return response.json() # Assuming the API returns JSON data | |
else: | |
return {"error": "Failed to fetch profile data"} | |
# Create a dropdown for task selection | |
task_options = ["Trends", "Script Generation", "Hashtag Generation", "Fetch TikTok Profile"] | |
# Create a dropdown for task selection | |
task_options = ["Trends", "Script Generation", "Hashtag Generation", "Fetch TikTok Profile"] | |
# Gradio interface | |
interface = Interface( | |
fn=handle_task, | |
inputs=[ | |
Dropdown(label="Select Task", choices=task_options), | |
Textbox(label="Enter Query", placeholder="Enter your query here...") | |
], | |
outputs=Markdown(label="Output"), | |
title="Multi-Task Social Media Assistant", | |
description="Choose a task (Trends, Script Generation, or Hashtag Generation) and enter your query to get tailored responses." | |
) | |
# Launch the interface | |
if __name__ == "__main__": | |
interface.launch(debug=True, share=True) | |