ManojINaik commited on
Commit
4b3c08b
·
verified ·
1 Parent(s): f80b8dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py CHANGED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from huggingface_hub import InferenceClient
4
+ import uvicorn
5
+
6
+ app = FastAPI()
7
+
8
+ # Initialize the InferenceClient with the specified model
9
+ client = InferenceClient("nvidia/Llama-3.1-Nemotron-70B-Instruct-HF")
10
+
11
+ # Define the structure of the request body
12
+ class CourseRequest(BaseModel):
13
+ course_name: str
14
+ history: list = [] # Keeping history optional
15
+ temperature: float = 0.0
16
+ max_new_tokens: int = 1048
17
+ top_p: float = 0.15
18
+ repetition_penalty: float = 1.0
19
+
20
+ # Format the prompt for the model
21
+ def format_prompt(course_name, history):
22
+ prompt = "<s>"
23
+ for user_prompt, bot_response in history:
24
+ prompt += f"[INST] {user_prompt} [/INST] {bot_response} </s> "
25
+ prompt += f"[INST] Generate a roadmap for the course: {course_name} [/INST]"
26
+ return prompt
27
+
28
+ # Generate text using the specified parameters
29
+ def generate(course_request: CourseRequest):
30
+ temperature = max(float(course_request.temperature), 1e-2)
31
+ top_p = float(course_request.top_p)
32
+
33
+ generate_kwargs = {
34
+ 'temperature': temperature,
35
+ 'max_new_tokens': course_request.max_new_tokens,
36
+ 'top_p': top_p,
37
+ 'repetition_penalty': course_request.repetition_penalty,
38
+ 'do_sample': True,
39
+ 'seed': 42,
40
+ }
41
+
42
+ formatted_prompt = format_prompt(course_request.course_name, course_request.history)
43
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
44
+ output = ""
45
+
46
+ for response in stream:
47
+ output += response.token.text
48
+ return output
49
+
50
+ # Define the API endpoint for generating course roadmaps
51
+ @app.post("/generate-roadmap/")
52
+ async def generate_roadmap(course_request: CourseRequest):
53
+ return {"roadmap": generate(course_request)}
54
+
55
+ # Run the application (uncomment the next two lines if running this as a standalone script)
56
+ # if __name__ == "__main__":
57
+ # uvicorn.run(app, host="0.0.0.0", port=8000)