Spaces:
Sleeping
Sleeping
import requests | |
from dotenv import load_dotenv | |
import os | |
load_dotenv() | |
huggingface = os.getenv("HUGGINGFACE") | |
class TopicGenerator: | |
def __init__(self): | |
# Initialize API-URL and authorization headers | |
self.url = "https://api-inference.huggingface.co/models/google/flan-t5-large" | |
self.headers = {"Authorization": f"Bearer {huggingface}"} | |
def query(self, payload): | |
response = requests.post(self.url, headers=self.headers, | |
json=payload) | |
return response | |
def generate_topics(self, user_input, num_topics=3): | |
payload = { | |
"inputs": f"""Generate a topic sentence idea based on the user input. | |
The generated topics should portray the context or idea behind the given sentences or phrase. | |
For Instance, | |
- "Grocery Shopping" OR "Grocery List" OR "Shopping List": "I'm going grocery shopping tomorrow, | |
and I would like to get the following things on my grocery list: Milk, Soybeans, Cowpeas, | |
Saturated Water, Onions, Tomatoes, etc." | |
- "Studying For Exams" OR "Exams Studies": "Exams aare coming up and I have to prepare for the core | |
courses. I'll be studying for Control Systems, Software Engineering and Circuit Theory." | |
- "Healthy Breakfast": "To prepare a healthy breakfast, I need the appropriate combination of balanced | |
diet. I'll need oats, yogurt, fresh berries, honey and smoothies." | |
- "Fitness Routine": "Starting a fitness routine involves workout clothes, running shoes, | |
a water bottles, and a gym membership. With this, I can start a proper fitness plan." | |
- "Summer Vacation": "Packing swimsuits and enjoy the view of the ocean." | |
- "Coffee Break": "Sipping Coffee at the table." | |
- "Relaxation": "Sitting at the table enjoying." | |
This is what I'm expecting the model to do. Here is the input: {user_input} | |
""", | |
"do_sample": True, | |
"temperature": 0.7, | |
"num_return_sequences": num_topics | |
} | |
output = self.query(payload) | |
if output.status_code == 200: | |
topic = output.json() | |
return topic | |
else: | |
return f"Error: Received response code {output.status_code}" | |