Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
from fuzzy_json import loads
|
2 |
from half_json.core import JSONFixer
|
3 |
-
from
|
4 |
from retry import retry
|
5 |
import re
|
6 |
from dotenv import load_dotenv
|
@@ -12,20 +12,25 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
12 |
|
13 |
# Retrieve environment variables
|
14 |
TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY")
|
|
|
15 |
|
16 |
SysPromptDefault = "You are an expert AI, complete the given task. Do not add any additional comments."
|
17 |
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."
|
|
|
|
|
|
|
18 |
|
19 |
@retry(tries=3, delay=1)
|
20 |
def together_response(message, model = "meta-llama/Llama-3-8b-chat-hf", SysPrompt = SysPromptDefault):
|
21 |
-
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
messages=messages,
|
28 |
-
temperature=0.2,
|
29 |
)
|
30 |
return response.choices[0].message.content
|
31 |
|
@@ -55,6 +60,12 @@ def generate_topics(user_input, num_topics, previous_queries):
|
|
55 |
subtopics = json_from_text(response_topics)
|
56 |
return subtopics
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
# Define the app
|
59 |
app = FastAPI()
|
60 |
|
@@ -72,6 +83,10 @@ class TopicInput(BaseModel):
|
|
72 |
num_topics: int = Query(default=5, description="Number of subtopics to generate (default: 5)")
|
73 |
previous_queries: list[str] = Query(default=[], description="List of previous queries for context")
|
74 |
|
|
|
|
|
|
|
|
|
75 |
@app.get("/", tags=["Home"])
|
76 |
def api_home():
|
77 |
return {'detail': 'Welcome to FastAPI Subtopics API! Visit https://pvanand-generate-subtopics.hf.space/docs to test'}
|
@@ -81,3 +96,8 @@ async def create_topics(input: TopicInput):
|
|
81 |
topics = generate_topics(input.user_input, input.num_topics, input.previous_queries)
|
82 |
return {"topics": topics}
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fuzzy_json import loads
|
2 |
from half_json.core import JSONFixer
|
3 |
+
from openai import OpenAI
|
4 |
from retry import retry
|
5 |
import re
|
6 |
from dotenv import load_dotenv
|
|
|
12 |
|
13 |
# Retrieve environment variables
|
14 |
TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY")
|
15 |
+
GROQ_API_KEY = "gsk_"+os.getenv("GROQ_API_KEY")
|
16 |
|
17 |
SysPromptDefault = "You are an expert AI, complete the given task. Do not add any additional comments."
|
18 |
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."
|
19 |
+
SysPromptJson = "You are now in the role of an expert AI who can extract structured information from user request. Both key and value pairs must be in double quotes. You must respond ONLY with a valid JSON file. Do not add any additional comments."
|
20 |
+
SysPromptMd = "You are an expert AI who can create a structured report using information provided in the context from user request.The report should be in markdown format consists of markdown tables structured into subtopics. Do not add any additional comments."
|
21 |
+
SysPromptMdOffline = "You are an expert AI who can create a structured report using your knowledge on user request.The report should be in markdown format consists of markdown tables/lists/paragraphs as needed, structured into subtopics. Do not add any additional comments."
|
22 |
|
23 |
@retry(tries=3, delay=1)
|
24 |
def together_response(message, model = "meta-llama/Llama-3-8b-chat-hf", SysPrompt = SysPromptDefault):
|
25 |
+
base_url_groq = "https://api.groq.com/openai/v1"
|
26 |
+
groq_model_name="llama3-8b-8192"
|
27 |
+
client = OpenAI(base_url= base_url_groq, api_key= GROQ_API_KEY)
|
28 |
+
messages=[{"role": "system", "content": SysPrompt},{"role": "user", "content": message}]
|
29 |
|
30 |
+
response = client.chat.completions.create(
|
31 |
+
model=groq_model_name,
|
32 |
+
messages=messages,
|
33 |
+
temperature=0.2,
|
|
|
|
|
34 |
)
|
35 |
return response.choices[0].message.content
|
36 |
|
|
|
60 |
subtopics = json_from_text(response_topics)
|
61 |
return subtopics
|
62 |
|
63 |
+
def generate_report(topic, description):
|
64 |
+
prompt = f"""create a detailed report on {topic} by following the instructions: {description}"""
|
65 |
+
md_report = together_response(prompt, model = "meta-llama/Llama-3-70b-chat-hf", SysPrompt = SysPromptMdOffline)
|
66 |
+
return md_to_html(md_report)
|
67 |
+
|
68 |
+
|
69 |
# Define the app
|
70 |
app = FastAPI()
|
71 |
|
|
|
83 |
num_topics: int = Query(default=5, description="Number of subtopics to generate (default: 5)")
|
84 |
previous_queries: list[str] = Query(default=[], description="List of previous queries for context")
|
85 |
|
86 |
+
class ReportInput(BaseModel):
|
87 |
+
topic: str = Query(description="The main topic for the report")
|
88 |
+
description: str = Query(description="A brief description of the topic")
|
89 |
+
|
90 |
@app.get("/", tags=["Home"])
|
91 |
def api_home():
|
92 |
return {'detail': 'Welcome to FastAPI Subtopics API! Visit https://pvanand-generate-subtopics.hf.space/docs to test'}
|
|
|
96 |
topics = generate_topics(input.user_input, input.num_topics, input.previous_queries)
|
97 |
return {"topics": topics}
|
98 |
|
99 |
+
@app.post("/generate_report")
|
100 |
+
async def create_report(input: ReportInput):
|
101 |
+
report = generate_report(input.topic, input.description) # You'll need to implement this function
|
102 |
+
return {"report": report}
|
103 |
+
|