Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,18 @@
|
|
| 1 |
from fastapi import FastAPI, Request, HTTPException
|
| 2 |
-
import requests
|
| 3 |
import os
|
| 4 |
from typing import Dict, Any
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
| 8 |
BOT_USERNAME = "@DiscussionBot"
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
@app.get("/")
|
| 13 |
async def root(request: Request) -> Dict[str, Any]:
|
|
@@ -45,39 +50,41 @@ async def webhook(request: Request) -> Dict[str, Any]:
|
|
| 45 |
):
|
| 46 |
print(f"Processing comment: {data['comment']['content']}")
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
else:
|
| 76 |
-
error_message = f"Failed to post comment: {comment_response.text}"
|
| 77 |
-
print(error_message)
|
| 78 |
-
raise HTTPException(status_code=500, detail=error_message)
|
| 79 |
else:
|
| 80 |
-
error_message = f"
|
| 81 |
print(error_message)
|
| 82 |
raise HTTPException(status_code=500, detail=error_message)
|
| 83 |
|
|
|
|
| 1 |
from fastapi import FastAPI, Request, HTTPException
|
|
|
|
| 2 |
import os
|
| 3 |
from typing import Dict, Any
|
| 4 |
+
from transformers import pipeline
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
# Initialisation du pipeline pour l'API d'inférence Hugging Face
|
| 9 |
BOT_USERNAME = "@DiscussionBot"
|
| 10 |
+
MODEL = "meta-llama/Llama-3.2-1B-Instruct"
|
| 11 |
+
try:
|
| 12 |
+
pipe = pipeline("text-generation", model=MODEL, token=os.getenv("HF_TOKEN"))
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print(f"Failed to initialize pipeline: {str(e)}")
|
| 15 |
+
raise
|
| 16 |
|
| 17 |
@app.get("/")
|
| 18 |
async def root(request: Request) -> Dict[str, Any]:
|
|
|
|
| 50 |
):
|
| 51 |
print(f"Processing comment: {data['comment']['content']}")
|
| 52 |
|
| 53 |
+
# Préparation du prompt au format "messages"
|
| 54 |
+
messages = [
|
| 55 |
+
{
|
| 56 |
+
"role": "user",
|
| 57 |
+
"content": f"Faites comme si vous étiez un robot qui répond aux discussions sur l'apprentissage automatique et répondez au commentaire suivant :\n{data['comment']['content']}"
|
| 58 |
+
}
|
| 59 |
+
]
|
| 60 |
|
| 61 |
+
# Inférence avec le pipeline
|
| 62 |
+
try:
|
| 63 |
+
output = pipe(messages, max_new_tokens=100, num_return_sequences=1)
|
| 64 |
+
print(f"Pipeline response: {output}")
|
| 65 |
+
continuation_text = output[0]["generated_text"][-1]["content"].strip()
|
| 66 |
+
except Exception as e:
|
| 67 |
+
error_message = f"Inference failed: {str(e)}"
|
| 68 |
+
print(error_message)
|
| 69 |
+
raise HTTPException(status_code=500, detail=error_message)
|
| 70 |
|
| 71 |
+
# Publication du commentaire
|
| 72 |
+
comment_url = data["discussion"]["url"]["api"] + "/comment"
|
| 73 |
+
print(f"Posting to comment URL: {comment_url}")
|
| 74 |
+
comment_response = requests.post(
|
| 75 |
+
comment_url,
|
| 76 |
+
headers={
|
| 77 |
+
"Authorization": f"Bearer {os.getenv('HF_TOKEN')}",
|
| 78 |
+
"Content-Type": "application/json",
|
| 79 |
+
},
|
| 80 |
+
json={"comment": continuation_text},
|
| 81 |
+
)
|
| 82 |
|
| 83 |
+
if comment_response.status_code == 200:
|
| 84 |
+
print("Comment posted successfully")
|
| 85 |
+
return {"success": True}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
else:
|
| 87 |
+
error_message = f"Failed to post comment: {comment_response.text}"
|
| 88 |
print(error_message)
|
| 89 |
raise HTTPException(status_code=500, detail=error_message)
|
| 90 |
|