Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
from pydantic import BaseModel
|
3 |
from huggingface_hub import InferenceClient
|
|
|
4 |
|
5 |
app = FastAPI()
|
6 |
|
@@ -11,29 +12,39 @@ class CourseRequest(BaseModel):
|
|
11 |
course_name: str
|
12 |
|
13 |
def format_prompt(course_name: str):
|
14 |
-
return f"
|
15 |
|
16 |
def generate_roadmap(item: CourseRequest):
|
17 |
prompt = format_prompt(item.course_name)
|
18 |
-
|
19 |
-
# You can adjust these parameters as needed
|
20 |
-
generate_kwargs = {
|
21 |
-
"temperature": 0.7,
|
22 |
-
"max_new_tokens": 150,
|
23 |
-
"top_p": 0.9,
|
24 |
-
"repetition_penalty": 1.1,
|
25 |
-
}
|
26 |
-
|
27 |
-
# Call the model to generate the roadmap
|
28 |
-
stream = client.text_generation(prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
29 |
output = ""
|
30 |
|
31 |
for response in stream:
|
32 |
output += response.token.text
|
33 |
-
|
34 |
return output
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
@app.post("/generate/")
|
37 |
async def generate_roadmap_endpoint(course_request: CourseRequest):
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
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")
|