Spaces:
Runtime error
Runtime error
v1.0a
Browse files- .gitignore +1 -0
- Dockerfile +24 -0
- README.md +1 -1
- app.py +80 -0
- requirements.txt +4 -0
- tools/bible_tools.py +72 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
Dockerfile
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.12.7
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
# Install system dependencies
|
6 |
+
RUN apt-get update && apt-get install -y \
|
7 |
+
build-essential \
|
8 |
+
curl \
|
9 |
+
&& rm -rf /var/lib/apt/lists/*
|
10 |
+
|
11 |
+
# Copy requirements first to leverage Docker cache
|
12 |
+
COPY requirements.txt .
|
13 |
+
|
14 |
+
# Install Python dependencies
|
15 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
16 |
+
|
17 |
+
# Copy the rest of the application
|
18 |
+
COPY . .
|
19 |
+
|
20 |
+
# Expose the port the app runs on
|
21 |
+
EXPOSE 7860
|
22 |
+
|
23 |
+
# Command to run the application
|
24 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: SwiftAgent
|
3 |
-
emoji:
|
4 |
colorFrom: yellow
|
5 |
colorTo: indigo
|
6 |
sdk: docker
|
|
|
1 |
---
|
2 |
title: SwiftAgent
|
3 |
+
emoji: 🪽
|
4 |
colorFrom: yellow
|
5 |
colorTo: indigo
|
6 |
sdk: docker
|
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from smolagents import CodeAgent, HfApiModel, tool, DuckDuckGoSearchTool
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from typing import List
|
5 |
+
import re
|
6 |
+
import os
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
# Load environment variables
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
class Query(BaseModel):
|
15 |
+
text: str
|
16 |
+
|
17 |
+
class SearchResponse(BaseModel):
|
18 |
+
urls: List[str]
|
19 |
+
full_response: str
|
20 |
+
|
21 |
+
@tool
|
22 |
+
def generate_bible_url(book_num: str, chapter: str, verse: str) -> str:
|
23 |
+
"""Generates a URL for a bible verse using the JW.org format
|
24 |
+
|
25 |
+
Args:
|
26 |
+
book_num: two digit book number (01-66)
|
27 |
+
chapter: three digit chapter number (padded with zeros)
|
28 |
+
verse: three digit verse number (padded with zeros)
|
29 |
+
"""
|
30 |
+
bible_code = f"{book_num.zfill(2)}{chapter.zfill(3)}{verse.zfill(3)}"
|
31 |
+
return f"https://www.jw.org/finder?srcid=jwlshare&wtlocale=E&prefer=lang&bible={bible_code}&pub=nwtsty"
|
32 |
+
|
33 |
+
# Initialize the agent with Qwen model and token
|
34 |
+
agent = CodeAgent(
|
35 |
+
tools=[generate_bible_url, DuckDuckGoSearchTool()],
|
36 |
+
model=HfApiModel(
|
37 |
+
"Qwen/Qwen2.5-Coder-32B-Instruct",
|
38 |
+
token=os.getenv("HUGGINGFACE_API_TOKEN")
|
39 |
+
),
|
40 |
+
additional_authorized_imports=[]
|
41 |
+
)
|
42 |
+
|
43 |
+
@app.post("/search", response_model=SearchResponse)
|
44 |
+
async def search_verses(query: Query):
|
45 |
+
try:
|
46 |
+
# Execute the agent with the query
|
47 |
+
result = agent.run(f"""You are a helpful AI assistant specializing in finding relevant Bible verses.
|
48 |
+
|
49 |
+
1. First use the DuckDuckGo search tool to search specifically for "{query.text} bible verse"
|
50 |
+
2. From the search results, identify the most relevant verse that directly addresses the query
|
51 |
+
3. Verify the verse's relevance before generating the URL
|
52 |
+
4. Use the generate_bible_url tool to create the URL for the chosen verse
|
53 |
+
|
54 |
+
Only return verses that specifically address: {query.text}
|
55 |
+
|
56 |
+
Query: {query.text}""")
|
57 |
+
|
58 |
+
# Extract URLs whether result is string or list
|
59 |
+
if isinstance(result, list):
|
60 |
+
# Handle list of tuples (url, description)
|
61 |
+
urls = [item[0] for item in result if isinstance(item, tuple) and len(item) > 0]
|
62 |
+
else:
|
63 |
+
# Handle string response
|
64 |
+
urls = re.findall(r'https://www\.jw\.org/finder\?[^\s\)]+', str(result))
|
65 |
+
|
66 |
+
return {
|
67 |
+
"urls": urls,
|
68 |
+
"full_response": str(result)
|
69 |
+
}
|
70 |
+
|
71 |
+
except Exception as e:
|
72 |
+
raise HTTPException(status_code=500, detail=str(e))
|
73 |
+
|
74 |
+
@app.get("/health")
|
75 |
+
async def health_check():
|
76 |
+
return {"status": "healthy"}
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
import uvicorn
|
80 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
smolagents>=0.0.4
|
2 |
+
fastapi>=0.100.0
|
3 |
+
uvicorn>=0.22.0
|
4 |
+
python-dotenv>=1.0.0
|
tools/bible_tools.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
BIBLE_BOOKS = {
|
2 |
+
"genesis": "01",
|
3 |
+
"exodus": "02",
|
4 |
+
"leviticus": "03",
|
5 |
+
"numbers": "04",
|
6 |
+
"deuteronomy": "05",
|
7 |
+
"joshua": "06",
|
8 |
+
"judges": "07",
|
9 |
+
"ruth": "08",
|
10 |
+
"1 samuel": "09",
|
11 |
+
"2 samuel": "10",
|
12 |
+
"1 kings": "11",
|
13 |
+
"2 kings": "12",
|
14 |
+
"1 chronicles": "13",
|
15 |
+
"2 chronicles": "14",
|
16 |
+
"ezra": "15",
|
17 |
+
"nehemiah": "16",
|
18 |
+
"esther": "17",
|
19 |
+
"job": "18",
|
20 |
+
"psalms": "19",
|
21 |
+
"proverbs": "20",
|
22 |
+
"ecclesiastes": "21",
|
23 |
+
"song of solomon": "22",
|
24 |
+
"isaiah": "23",
|
25 |
+
"jeremiah": "24",
|
26 |
+
"lamentations": "25",
|
27 |
+
"ezekiel": "26",
|
28 |
+
"daniel": "27",
|
29 |
+
"hosea": "28",
|
30 |
+
"joel": "29",
|
31 |
+
"amos": "30",
|
32 |
+
"obadiah": "31",
|
33 |
+
"jonah": "32",
|
34 |
+
"micah": "33",
|
35 |
+
"nahum": "34",
|
36 |
+
"habakkuk": "35",
|
37 |
+
"zephaniah": "36",
|
38 |
+
"haggai": "37",
|
39 |
+
"zechariah": "38",
|
40 |
+
"malachi": "39",
|
41 |
+
"matthew": "40",
|
42 |
+
"mark": "41",
|
43 |
+
"luke": "42",
|
44 |
+
"john": "43",
|
45 |
+
"acts": "44",
|
46 |
+
"romans": "45",
|
47 |
+
"1 corinthians": "46",
|
48 |
+
"2 corinthians": "47",
|
49 |
+
"galatians": "48",
|
50 |
+
"ephesians": "49",
|
51 |
+
"philippians": "50",
|
52 |
+
"colossians": "51",
|
53 |
+
"1 thessalonians": "52",
|
54 |
+
"2 thessalonians": "53",
|
55 |
+
"1 timothy": "54",
|
56 |
+
"2 timothy": "55",
|
57 |
+
"titus": "56",
|
58 |
+
"philemon": "57",
|
59 |
+
"hebrews": "58",
|
60 |
+
"james": "59",
|
61 |
+
"1 peter": "60",
|
62 |
+
"2 peter": "61",
|
63 |
+
"1 john": "62",
|
64 |
+
"2 john": "63",
|
65 |
+
"3 john": "64",
|
66 |
+
"jude": "65",
|
67 |
+
"revelation": "66"
|
68 |
+
}
|
69 |
+
|
70 |
+
def get_book_number(book_name: str) -> str:
|
71 |
+
"""Convert book name to its number"""
|
72 |
+
return BIBLE_BOOKS.get(book_name.lower(), "")
|