Created
October 1, 2024 19:30
-
-
Save artydev/fea2f0b82df13b64c5ad20a4b3a0f764 to your computer and use it in GitHub Desktop.
Albert FastApi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from fastapi import FastAPI, HTTPException | |
from pydantic import BaseModel | |
from openai import OpenAI | |
from typing import Optional | |
from fastapi.middleware.cors import CORSMiddleware | |
app = FastAPI() | |
class UserMessage(BaseModel): | |
message: str | |
base_url = "https://albert.api.etalab.gouv.fr/v1/" | |
api_key = " -iR25fnq57fE54fDjjKpEDYZa298973CmAPiv7F67CX58funu7NWdpRp6b599NKPY" | |
from openai import OpenAI | |
client = OpenAI(base_url=base_url, api_key=api_key) | |
origins = ["*" | |
] | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=origins, | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
@app.post("/generate_summary") | |
async def generate_summary(user_message: UserMessage): | |
try: | |
data = { | |
"model": "AgentPublic/llama3-instruct-8b", | |
"messages": [{"role": "user", "content": user_message.message}], | |
"stream": False, | |
"max_tokens": 4000, | |
"n": 1, | |
} | |
response = client.chat.completions.create(**data) | |
summary = response.choices[0].message.content | |
return {"summary": summary} # Limit the summary to 500 characters | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}") | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="0.0.0.0", port=8000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Summary Generator</title> | |
<style> | |
body { | |
font-family: Arial, sans-serif; | |
max-width: 600px; | |
margin: 40px auto; | |
padding: 20px; | |
background-color: #f0f0f0; | |
border-radius: 10px; | |
box-shadow: 0 0 10px rgba(0,0,0,0.1); | |
} | |
label { | |
display: block; | |
margin-bottom: 10px; | |
} | |
textarea { | |
width: 100%; | |
height: 150px; | |
margin-bottom: 15px; | |
padding: 10px; | |
resize: vertical; | |
} | |
button { | |
padding: 10px 20px; | |
background-color: #007bff; | |
color: white; | |
border: none; | |
cursor: pointer; | |
} | |
button:hover { | |
background-color: #0056b3; | |
} | |
.result { | |
margin-top: 20px; | |
padding: 15px; | |
background-color: #ffffff; | |
border: 1px solid #ddd; | |
border-radius: 5px; | |
} | |
.loading { | |
visibility: hidden; | |
opacity: 0; | |
transition: opacity 0.3s ease; | |
} | |
.loading.show { | |
visibility: visible; | |
opacity: 1; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Summary Generator</h1> | |
<form id="summaryForm"> | |
<label for="message">Enter your text:</label> | |
<textarea id="message" required></textarea> | |
<button type="submit">Generate Summary</button> | |
</form> | |
<div class="loading" id="loadingIndicator">Loading...</div> | |
<div class="result" id="result"></div> | |
<script> | |
const form = document.getElementById('summaryForm'); | |
const messageInput = document.getElementById('message'); | |
const loadingIndicator = document.getElementById('loadingIndicator'); | |
const resultDiv = document.getElementById('result'); | |
form.addEventListener('submit', async (e) => { | |
e.preventDefault(); | |
const message = messageInput.value.trim(); | |
// Sanitize input | |
const sanitizedMessage = message.replace(/</g, '<').replace(/>/g, '>'); | |
try { | |
// Show loading indicator | |
loadingIndicator.classList.add('show'); | |
// Make API call | |
const response = await fetch(' http://127.0.0.1:8000/generate_summary', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify({ message: sanitizedMessage }), | |
}); | |
if (!response.ok) { | |
throw new Error(`HTTP error! status: ${response.status}`); | |
} | |
const data = await response.json(); | |
// Display result | |
resultDiv.innerHTML = `<h2>Summary:</h2><p>${data.summary}</p>`; | |
} catch (error) { | |
console.error('Error:', error); | |
resultDiv.innerHTML = `<h2>Error</h2><p>An error occurred: ${error.message}</p>`; | |
} finally { | |
// Hide loading indicator | |
loadingIndicator.classList.remove('show'); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment