Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,57 +1,39 @@
|
|
| 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
|
| 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 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
'do_sample': True,
|
| 39 |
-
'seed': 42,
|
| 40 |
}
|
| 41 |
|
| 42 |
-
|
| 43 |
-
stream = client.text_generation(
|
| 44 |
output = ""
|
| 45 |
|
| 46 |
for response in stream:
|
| 47 |
output += response.token.text
|
|
|
|
| 48 |
return output
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
return {"roadmap":
|
| 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)
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from huggingface_hub import InferenceClient
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
+
# Initialize the inference client for the AI model
|
| 8 |
client = InferenceClient("nvidia/Llama-3.1-Nemotron-70B-Instruct-HF")
|
| 9 |
|
|
|
|
| 10 |
class CourseRequest(BaseModel):
|
| 11 |
course_name: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def format_prompt(course_name: str):
|
| 14 |
+
return f"Please generate a detailed roadmap for the course '{course_name}'. Include key topics, recommended resources, and a learning timeline."
|
| 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 |
+
roadmap = generate_roadmap(course_request)
|
| 39 |
+
return {"roadmap": roadmap}
|
|
|
|
|
|
|
|
|
|
|
|