Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,50 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
from pydantic import BaseModel
|
|
|
|
|
3 |
|
4 |
app = FastAPI()
|
5 |
|
|
|
|
|
|
|
6 |
class CourseRequest(BaseModel):
|
7 |
course_name: str
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
@app.post("/generate/")
|
10 |
-
async def
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Response
|
2 |
from pydantic import BaseModel
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
import graphviz
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
+
# Initialize the inference client for the AI model
|
9 |
+
client = InferenceClient("nvidia/Llama-3.1-Nemotron-70B-Instruct-HF")
|
10 |
+
|
11 |
class CourseRequest(BaseModel):
|
12 |
course_name: str
|
13 |
|
14 |
+
def format_prompt(course_name: str):
|
15 |
+
return f"As an expert in education, please generate a detailed roadmap for the course '{course_name}'. Include key topics."
|
16 |
+
|
17 |
+
def generate_roadmap(item: CourseRequest):
|
18 |
+
prompt = format_prompt(item.course_name)
|
19 |
+
stream = client.text_generation(prompt, max_new_tokens=200)
|
20 |
+
output = ""
|
21 |
+
|
22 |
+
for response in stream:
|
23 |
+
output += response.token.text
|
24 |
+
|
25 |
+
return output
|
26 |
+
|
27 |
+
def create_diagram(roadmap_text: str):
|
28 |
+
dot = graphviz.Digraph()
|
29 |
+
|
30 |
+
# Split the roadmap text into lines or sections for diagram creation
|
31 |
+
lines = roadmap_text.split('\n')
|
32 |
+
for i, line in enumerate(lines):
|
33 |
+
dot.node(str(i), line.strip()) # Create a node for each topic
|
34 |
+
|
35 |
+
if i > 0:
|
36 |
+
dot.edge(str(i - 1), str(i)) # Connect nodes sequentially
|
37 |
+
|
38 |
+
return dot
|
39 |
+
|
40 |
@app.post("/generate/")
|
41 |
+
async def generate_roadmap_endpoint(course_request: CourseRequest):
|
42 |
+
roadmap_text = generate_roadmap(course_request)
|
43 |
+
diagram = create_diagram(roadmap_text)
|
44 |
+
|
45 |
+
# Render the diagram to a PNG image
|
46 |
+
diagram_path = "/tmp/roadmap"
|
47 |
+
diagram.render(diagram_path, format='png', cleanup=True)
|
48 |
+
|
49 |
+
with open(diagram_path + ".png", "rb") as f:
|
50 |
+
return Response(content=f.read(), media_type="image/png")
|