Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from fastapi import FastAPI, Request
|
| 5 |
+
from fastapi.responses import JSONResponse
|
| 6 |
+
import uvicorn
|
| 7 |
+
|
| 8 |
+
# Configuration
|
| 9 |
+
API_URL = "https://router.huggingface.co/v1/chat/completions"
|
| 10 |
+
headers = {
|
| 11 |
+
"Authorization": f"Bearer {os.environ.get('HF_TOKEN', '')}",
|
| 12 |
+
}
|
| 13 |
+
MODEL = "deepseek-ai/DeepSeek-V3.2:novita"
|
| 14 |
+
|
| 15 |
+
app = FastAPI()
|
| 16 |
+
|
| 17 |
+
def query_ai(prompt):
|
| 18 |
+
payload = {
|
| 19 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 20 |
+
"model": MODEL
|
| 21 |
+
}
|
| 22 |
+
try:
|
| 23 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
|
| 24 |
+
return response.json()
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return {"error": str(e)}
|
| 27 |
+
|
| 28 |
+
def search_osm_logic(query, location="Nigeria"):
|
| 29 |
+
params = {
|
| 30 |
+
"q": f"{query} in {location}",
|
| 31 |
+
"format": "json",
|
| 32 |
+
"addressdetails": 1,
|
| 33 |
+
"limit": 10
|
| 34 |
+
}
|
| 35 |
+
try:
|
| 36 |
+
resp = requests.get("https://nominatim.openstreetmap.org/search", params=params, headers={"User-Agent": "Internly/1.0"}, timeout=10)
|
| 37 |
+
return resp.json()
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return []
|
| 40 |
+
|
| 41 |
+
@app.get("/api/search")
|
| 42 |
+
async def api_search(q: str, loc: str = "Nigeria"):
|
| 43 |
+
osm_results = search_osm_logic(q, loc)
|
| 44 |
+
enriched_results = []
|
| 45 |
+
|
| 46 |
+
for item in osm_results:
|
| 47 |
+
name = item.get("display_name").split(",")[0]
|
| 48 |
+
address = item.get("display_name")
|
| 49 |
+
lat = item.get("lat")
|
| 50 |
+
lon = item.get("lon")
|
| 51 |
+
osm_id = item.get("osm_id")
|
| 52 |
+
|
| 53 |
+
prompt = (
|
| 54 |
+
f"Analyze '{name}' at '{address}' for SIWES internship in Nigeria. "
|
| 55 |
+
"Return ONLY a JSON object with: 'orgType' (Private/Government/Unknown), "
|
| 56 |
+
"'siwesCategory' (e.g. Healthcare, Engineering), 'summary' (1 sentence), 'relevance' (1 sentence)."
|
| 57 |
+
)
|
| 58 |
+
ai_resp = query_ai(prompt)
|
| 59 |
+
|
| 60 |
+
result = {
|
| 61 |
+
"osmId": str(osm_id),
|
| 62 |
+
"name": name,
|
| 63 |
+
"address": address,
|
| 64 |
+
"location": loc,
|
| 65 |
+
"coordinates": {"lat": float(lat) if lat else 0, "lon": float(lon) if lon else 0},
|
| 66 |
+
"type": item.get("type", "unknown")
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
content = ai_resp["choices"][0]["message"]["content"]
|
| 71 |
+
# Basic JSON extraction if model wraps in markdown
|
| 72 |
+
if "```json" in content:
|
| 73 |
+
content = content.split("```json")[1].split("```")[0]
|
| 74 |
+
import json
|
| 75 |
+
ai_data = json.loads(content.strip())
|
| 76 |
+
result.update(ai_data)
|
| 77 |
+
except:
|
| 78 |
+
result.update({
|
| 79 |
+
"orgType": "Unknown",
|
| 80 |
+
"siwesCategory": "Unknown",
|
| 81 |
+
"summary": "AI enrichment failed.",
|
| 82 |
+
"relevance": "Unknown"
|
| 83 |
+
})
|
| 84 |
+
|
| 85 |
+
enriched_results.append(result)
|
| 86 |
+
|
| 87 |
+
return enriched_results
|
| 88 |
+
|
| 89 |
+
# Gradio Interface for testing
|
| 90 |
+
def gradio_search(query, location):
|
| 91 |
+
import asyncio
|
| 92 |
+
import json
|
| 93 |
+
# Use the same logic for the UI
|
| 94 |
+
results = search_osm_logic(query, location)
|
| 95 |
+
formatted = []
|
| 96 |
+
for r in results:
|
| 97 |
+
formatted.append(f"### {r.get('display_name')}\n")
|
| 98 |
+
return "\n".join(formatted) if formatted else "No results found."
|
| 99 |
+
|
| 100 |
+
with gr.Blocks(title="Internly API Service") as demo:
|
| 101 |
+
gr.Markdown("# Internly API Service")
|
| 102 |
+
gr.Markdown("This space serves as the backend API for Internly. Search is exposed at `/api/search`.")
|
| 103 |
+
|
| 104 |
+
with gr.Row():
|
| 105 |
+
q_input = gr.Textbox(label="Query")
|
| 106 |
+
l_input = gr.Textbox(label="Location", value="Nigeria")
|
| 107 |
+
|
| 108 |
+
btn = gr.Button("Test Search")
|
| 109 |
+
output = gr.Markdown()
|
| 110 |
+
|
| 111 |
+
btn.click(fn=gradio_search, inputs=[q_input, l_input], outputs=output)
|
| 112 |
+
|
| 113 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|