Noor22Tak commited on
Commit
8524bc2
·
verified ·
1 Parent(s): 94dc0ce

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +18 -0
  2. app.py +55 -0
  3. news_dataset.csv +0 -0
  4. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use official Python image as base
2
+ FROM python:3.10
3
+
4
+ # Set working directory in container
5
+ WORKDIR /app
6
+
7
+ # Copy requirements file and install dependencies
8
+ COPY requirements.txt .
9
+ RUN pip install --no-cache-dir -r requirements.txt
10
+
11
+ # Copy application files
12
+ COPY . .
13
+
14
+ # Expose port 7860 for FastAPI
15
+ EXPOSE 7860
16
+
17
+ # Command to run FastAPI app
18
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ import pandas as pd
4
+ import numpy as np
5
+ import faiss
6
+ import requests
7
+ import os
8
+
9
+ app = FastAPI()
10
+
11
+ # Load dataset
12
+ df = pd.read_csv("news_dataset.csv")
13
+
14
+ HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY") # Load from environment variable
15
+
16
+ # Load FAISS index
17
+ index = faiss.read_index("arabic_news_index")
18
+
19
+ # Define request model
20
+ class NewsQuery(BaseModel):
21
+ prompt: str
22
+
23
+ def create_textual_representation(row):
24
+ """Convert a news article into a structured text representation."""
25
+ return f"""
26
+ الكاتب: {row['writer']},
27
+ الموقع: {row['location']},
28
+ التاريخ: {row['date']},
29
+ الوقت: {row['time']},
30
+ العنوان: {row['title']},
31
+ الخبر: {row['news']}
32
+ """
33
+
34
+ @app.post("/recommend")
35
+ async def recommend_articles(query: NewsQuery):
36
+ """Find similar news articles using FAISS with real Llama 3.1 embeddings."""
37
+
38
+ # Call Llama 3.1 remotely for embeddings
39
+ res = requests.post("https://api-inference.huggingface.co/models/meta-llama/Llama-3.1-8B",
40
+ headers={"Authorization": HUGGINGFACE_API_KEY},
41
+ json={"inputs": query.prompt})
42
+
43
+ if res.status_code != 200:
44
+ return {"error": "Failed to get embeddings from Llama 3.1"}
45
+
46
+ # Extract the real embedding
47
+ embedding = np.array([res.json()[0]['embedding']], dtype="float32")
48
+
49
+ # Search FAISS index for similar articles
50
+ D, I = index.search(embedding, 5)
51
+
52
+ # Retrieve recommended articles
53
+ recommendations = df.iloc[I.flatten()][['title', 'writer', 'news']].to_dict(orient="records")
54
+
55
+ return {"recommendations": recommendations}
news_dataset.csv ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ pandas
4
+ numpy
5
+ faiss-cpu
6
+ requests