Spaces:
Sleeping
Sleeping
saifeddinemk
commited on
Commit
•
bc119db
1
Parent(s):
a510792
Fixed app v2
Browse files
app.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
from transformers import pipeline
|
4 |
-
from typing import List
|
5 |
|
6 |
# Initialize the FastAPI app
|
7 |
app = FastAPI()
|
8 |
|
9 |
-
# Initialize the text generation pipeline
|
10 |
-
pipe = pipeline("text-generation", model="
|
11 |
|
12 |
# Define the input schema for FastAPI
|
13 |
class Message(BaseModel):
|
@@ -20,13 +20,13 @@ class MessagesInput(BaseModel):
|
|
20 |
@app.post("/generate/")
|
21 |
async def generate_response(messages_input: MessagesInput):
|
22 |
try:
|
23 |
-
# Convert messages to the expected format
|
24 |
messages = [{"role": msg.role, "content": msg.content} for msg in messages_input.messages]
|
25 |
|
26 |
# Generate response using the pipeline
|
27 |
response = pipe(messages)
|
28 |
|
29 |
-
# Extract generated text
|
30 |
generated_text = response[0]["generated_text"]
|
31 |
|
32 |
return {
|
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
from transformers import pipeline
|
4 |
+
from typing import List
|
5 |
|
6 |
# Initialize the FastAPI app
|
7 |
app = FastAPI()
|
8 |
|
9 |
+
# Initialize the text generation pipeline with the specified model
|
10 |
+
pipe = pipeline("text-generation", model="jcordon5/Mistral-7B-cybersecurity-rules")
|
11 |
|
12 |
# Define the input schema for FastAPI
|
13 |
class Message(BaseModel):
|
|
|
20 |
@app.post("/generate/")
|
21 |
async def generate_response(messages_input: MessagesInput):
|
22 |
try:
|
23 |
+
# Convert the messages to the expected format for the pipeline
|
24 |
messages = [{"role": msg.role, "content": msg.content} for msg in messages_input.messages]
|
25 |
|
26 |
# Generate response using the pipeline
|
27 |
response = pipe(messages)
|
28 |
|
29 |
+
# Extract generated text from the response
|
30 |
generated_text = response[0]["generated_text"]
|
31 |
|
32 |
return {
|