import requests from fastapi import FastAPI import json import random from pydantic import BaseModel import re from data import topics app = FastAPI() url = "https://yashxx07-hf-llm-api.hf.space/api/v1/chat/completions" @app.get("/") def root(): return {"Status": "OK"} @app.get("/give-question") async def stream_gen(): s = requests.session() random_index = random.randint(0,len(topics)-1) promt = f"{random.randint(1,1000)}"+ f"give a easy random trivia question based on the topic '{topics[random_index]}' , give output as json object only following format: "+"{question: , options: [list of 4 options] , answer_index: }" body = { "model": "mixtral-8x7b", "messages": [ { "role": "user", "content": promt } ], "temperature": 0.5, "top_p": 0.95, "max_tokens": 100, "use_cache": False, "stream": False } header = {"content-type": "application/json"} resp = s.post(url, headers=header, json=body, stream = False) try: return {"data":json.loads(resp.json()["choices"][0]["message"]["content"])} except: return {"data": None}