Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -15,17 +15,6 @@ TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY")
|
|
| 15 |
SysPromptDefault = "You are an expert AI, complete the given task. Do not add any additional comments."
|
| 16 |
SysPromptList = "You are now in the role of an expert AI who can extract structured information from user request. All elements must be in double quotes. You must respond ONLY with a valid python List. Do not add any additional comments."
|
| 17 |
|
| 18 |
-
# Import FastAPI and other necessary libraries
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# Define the app
|
| 22 |
-
app = FastAPI()
|
| 23 |
-
|
| 24 |
-
# Create a Pydantic model to handle the input data
|
| 25 |
-
class TopicInput(BaseModel):
|
| 26 |
-
user_input: str
|
| 27 |
-
num_topics: int
|
| 28 |
-
|
| 29 |
@retry(tries=3, delay=1)
|
| 30 |
def together_response(message, model = "meta-llama/Llama-3-8b-chat-hf", SysPrompt = SysPromptDefault):
|
| 31 |
client = Together(api_key=TOGETHER_API_KEY)
|
|
@@ -56,10 +45,8 @@ def json_from_text(text):
|
|
| 56 |
fix_json = JSONFixer()
|
| 57 |
return loads(fix_json.fix(json_out).line)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
def generate_topics(user_input,num_topics):
|
| 62 |
-
prompt = f"""create a list of {num_topics} subtopics to follow for conducting {user_input}, RETURN VALID PYTHON LIST"""
|
| 63 |
response_topics = together_response(prompt, model = "meta-llama/Llama-3-8b-chat-hf", SysPrompt = SysPromptList)
|
| 64 |
subtopics = json_from_text(response_topics)
|
| 65 |
return subtopics
|
|
@@ -72,6 +59,16 @@ app.add_middleware(
|
|
| 72 |
allow_headers=["*"],
|
| 73 |
)
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
@app.get("/",tags=["Home"])
|
| 76 |
def api_home():
|
| 77 |
return {'detail': 'Welcome to FastAPI Subtopics API! /n visit https://pvanand-generate-subtopics.hf.space/docs to test'}
|
|
@@ -79,6 +76,6 @@ def api_home():
|
|
| 79 |
|
| 80 |
@app.post("/generate_topics/")
|
| 81 |
async def create_topics(input: TopicInput):
|
| 82 |
-
topics = generate_topics(input.user_input, input.num_topics)
|
| 83 |
return {"topics": topics}
|
| 84 |
|
|
|
|
| 15 |
SysPromptDefault = "You are an expert AI, complete the given task. Do not add any additional comments."
|
| 16 |
SysPromptList = "You are now in the role of an expert AI who can extract structured information from user request. All elements must be in double quotes. You must respond ONLY with a valid python List. Do not add any additional comments."
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
@retry(tries=3, delay=1)
|
| 19 |
def together_response(message, model = "meta-llama/Llama-3-8b-chat-hf", SysPrompt = SysPromptDefault):
|
| 20 |
client = Together(api_key=TOGETHER_API_KEY)
|
|
|
|
| 45 |
fix_json = JSONFixer()
|
| 46 |
return loads(fix_json.fix(json_out).line)
|
| 47 |
|
| 48 |
+
def generate_topics(user_input,num_topics,previous_query):
|
| 49 |
+
prompt = f"""create a list of {num_topics} subtopics to follow for conducting {user_input} in the context of {previous_query}, RETURN VALID PYTHON LIST"""
|
|
|
|
|
|
|
| 50 |
response_topics = together_response(prompt, model = "meta-llama/Llama-3-8b-chat-hf", SysPrompt = SysPromptList)
|
| 51 |
subtopics = json_from_text(response_topics)
|
| 52 |
return subtopics
|
|
|
|
| 59 |
allow_headers=["*"],
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# Define the app
|
| 63 |
+
app = FastAPI()
|
| 64 |
+
|
| 65 |
+
# Create a Pydantic model to handle the input data
|
| 66 |
+
class TopicInput(BaseModel):
|
| 67 |
+
user_input: str
|
| 68 |
+
num_topics: int
|
| 69 |
+
previous_query: str
|
| 70 |
+
|
| 71 |
+
|
| 72 |
@app.get("/",tags=["Home"])
|
| 73 |
def api_home():
|
| 74 |
return {'detail': 'Welcome to FastAPI Subtopics API! /n visit https://pvanand-generate-subtopics.hf.space/docs to test'}
|
|
|
|
| 76 |
|
| 77 |
@app.post("/generate_topics/")
|
| 78 |
async def create_topics(input: TopicInput):
|
| 79 |
+
topics = generate_topics(input.user_input, input.num_topics, input.num_topics.previous_query)
|
| 80 |
return {"topics": topics}
|
| 81 |
|