Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from typing import Dict
|
| 3 |
+
import logging
|
| 4 |
+
import httpx
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 9 |
+
|
| 10 |
+
# Define the Gradio API endpoint
|
| 11 |
+
gradio_api_url = "https://astro21-test-2.hf.space/--replicas/x5m8s/"
|
| 12 |
+
|
| 13 |
+
# Function to summarize a file using the Gradio API
|
| 14 |
+
async def summarize_file_with_gradio(file_content: str) -> Dict[str, str]:
|
| 15 |
+
try:
|
| 16 |
+
async with httpx.AsyncClient() as client:
|
| 17 |
+
response = await client.post(f"{gradio_api_url}/predict", json={"file_content": file_content})
|
| 18 |
+
if response.status_code == 200:
|
| 19 |
+
result = response.json()
|
| 20 |
+
return {"summary": result[0]}
|
| 21 |
+
else:
|
| 22 |
+
return {"error": "Gradio API request failed"}
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return {"error": str(e)}
|
| 25 |
+
|
| 26 |
+
# Define a route to handle summarization requests
|
| 27 |
+
@app.post('/summarize', response_model=Dict[str, str])
|
| 28 |
+
async def summarize_text(data: Dict[str, str]):
|
| 29 |
+
file_content = data.get('file_content')
|
| 30 |
+
result = await summarize_file_with_gradio(file_content)
|
| 31 |
+
return result
|
| 32 |
+
|
| 33 |
+
if __name__ == '__main__':
|
| 34 |
+
import uvicorn
|
| 35 |
+
uvicorn.run(app)
|