Spaces:
Runtime error
Runtime error
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fuzzy_json import loads
|
2 |
+
from half_json.core import JSONFixer
|
3 |
+
from together import Together
|
4 |
+
from retry import retry
|
5 |
+
import re
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
import os
|
8 |
+
from fastapi import FastAPI
|
9 |
+
from pydantic import BaseModel
|
10 |
+
|
11 |
+
# Retrieve environment variables
|
12 |
+
TOGETHER_API_KEY = "8e4274ffef010b6dd4b4343ed4a3158292691507f02ce99a58e09a0bd5400eab"
|
13 |
+
|
14 |
+
SysPromptDefault = "You are an expert AI, complete the given task. Do not add any additional comments."
|
15 |
+
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."
|
16 |
+
|
17 |
+
# Import FastAPI and other necessary libraries
|
18 |
+
|
19 |
+
|
20 |
+
# Define the app
|
21 |
+
app = FastAPI()
|
22 |
+
|
23 |
+
# Create a Pydantic model to handle the input data
|
24 |
+
class TopicInput(BaseModel):
|
25 |
+
user_input: str
|
26 |
+
num_topics: int
|
27 |
+
|
28 |
+
@retry(tries=3, delay=1)
|
29 |
+
def together_response(message, model = "meta-llama/Llama-3-8b-chat-hf", SysPrompt = SysPromptDefault):
|
30 |
+
client = Together(api_key=TOGETHER_API_KEY)
|
31 |
+
|
32 |
+
messages=[{"role": "system", "content": SysPrompt},{"role": "user", "content": message}]
|
33 |
+
|
34 |
+
response = client.chat.completions.create(
|
35 |
+
model=model,
|
36 |
+
messages=messages,
|
37 |
+
temperature=0.2,
|
38 |
+
)
|
39 |
+
return response.choices[0].message.content
|
40 |
+
|
41 |
+
def json_from_text(text):
|
42 |
+
"""
|
43 |
+
Extracts JSON from text using regex and fuzzy JSON loading.
|
44 |
+
"""
|
45 |
+
match = re.search(r'\{[\s\S]*\}', text)
|
46 |
+
if match:
|
47 |
+
json_out = match.group(0)
|
48 |
+
else:
|
49 |
+
json_out = text
|
50 |
+
try:
|
51 |
+
# Using fuzzy json loader
|
52 |
+
return loads(json_out)
|
53 |
+
except Exception:
|
54 |
+
# Using JSON fixer/ Fixes even half json/ Remove if you need an exception
|
55 |
+
fix_json = JSONFixer()
|
56 |
+
return loads(fix_json.fix(json_out).line)
|
57 |
+
|
58 |
+
SysPromptDefault = "You are an expert AI, complete the given task. Do not add any additional comments."
|
59 |
+
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."
|
60 |
+
def generate_topics(user_input,num_topics):
|
61 |
+
prompt = f"""create a list of {num_topics} subtopics to follow for conducting {user_input}, RETURN VALID PYTHON LIST"""
|
62 |
+
response_topics = together_response(prompt, model = "meta-llama/Llama-3-8b-chat-hf", SysPrompt = SysPromptList)
|
63 |
+
subtopics = json_from_text(response_topics)
|
64 |
+
return subtopics
|
65 |
+
|
66 |
+
@app.post("/generate_topics/")
|
67 |
+
async def create_topics(input: TopicInput):
|
68 |
+
topics = generate_topics(input.user_input, input.num_topics)
|
69 |
+
return {"topics": topics}
|
70 |
+
|