Update main.py
Browse files
main.py
CHANGED
|
@@ -1,32 +1,31 @@
|
|
| 1 |
-
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
-
from fastapi.staticfiles import StaticFiles
|
| 4 |
-
from fastapi.templating import Jinja2Templates
|
| 5 |
from fastapi.responses import FileResponse
|
| 6 |
-
import
|
|
|
|
| 7 |
import base64
|
|
|
|
|
|
|
| 8 |
from typing import Iterator
|
| 9 |
-
import os
|
| 10 |
from text_generation import Client
|
| 11 |
from deep_translator import GoogleTranslator
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
|
|
|
| 15 |
model_id = 'codellama/CodeLlama-34b-Instruct-hf'
|
| 16 |
-
|
| 17 |
API_URL = "https://api-inference.huggingface.co/models/" + model_id
|
| 18 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
API_URL,
|
| 22 |
-
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 23 |
-
)
|
| 24 |
EOS_STRING = "</s>"
|
| 25 |
EOT_STRING = "<EOT>"
|
| 26 |
|
| 27 |
-
|
| 28 |
-
def get_prompt(message: str, chat_history: list[tuple[str, str]],
|
| 29 |
-
system_prompt: str) -> str:
|
| 30 |
texts = [f'<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n']
|
| 31 |
do_strip = False
|
| 32 |
for user_input, response in chat_history:
|
|
@@ -37,33 +36,7 @@ def get_prompt(message: str, chat_history: list[tuple[str, str]],
|
|
| 37 |
texts.append(f'{message} [/INST]')
|
| 38 |
return ''.join(texts)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
def run(message: str,
|
| 42 |
-
chat_history: list[tuple[str, str]],
|
| 43 |
-
system_prompt: str,
|
| 44 |
-
max_new_tokens: int = 1024,
|
| 45 |
-
temperature: float = 0.1,
|
| 46 |
-
top_p: float = 0.9,
|
| 47 |
-
top_k: int = 50) -> Iterator[str]:
|
| 48 |
-
prompt = get_prompt(message, chat_history, system_prompt)
|
| 49 |
-
|
| 50 |
-
generate_kwargs = dict(
|
| 51 |
-
max_new_tokens=max_new_tokens,
|
| 52 |
-
do_sample=True,
|
| 53 |
-
top_p=top_p,
|
| 54 |
-
top_k=top_k,
|
| 55 |
-
temperature=temperature,
|
| 56 |
-
)
|
| 57 |
-
stream = client.generate_stream(prompt, **generate_kwargs)
|
| 58 |
-
output = ""
|
| 59 |
-
for response in stream:
|
| 60 |
-
if any([end_token in response.token.text for end_token in [EOS_STRING, EOT_STRING]]):
|
| 61 |
-
yield output
|
| 62 |
-
output = ""
|
| 63 |
-
else:
|
| 64 |
-
output += response.token.text
|
| 65 |
-
|
| 66 |
-
|
| 67 |
def generate_image_caption(image_data):
|
| 68 |
image_base64 = base64.b64encode(image_data).decode('utf-8')
|
| 69 |
payload = {"data": ["data:image/jpeg;base64," + image_base64]}
|
|
@@ -74,36 +47,60 @@ def generate_image_caption(image_data):
|
|
| 74 |
else:
|
| 75 |
return "Error: Unable to generate caption"
|
| 76 |
|
| 77 |
-
|
| 78 |
-
import random
|
| 79 |
-
|
| 80 |
-
from fastapi import BackgroundTasks
|
| 81 |
-
|
| 82 |
@app.post("/generate-story/")
|
| 83 |
async def generate_story(image_file: UploadFile = File(...), target_language: str = Form(...), background_tasks: BackgroundTasks = BackgroundTasks()):
|
|
|
|
| 84 |
image_data = await image_file.read()
|
|
|
|
|
|
|
| 85 |
system_prompt = f"write an attractive story in 300 words about {random.choice(['an adventurous journey', 'a mysterious encounter', 'a heroic quest'])}"
|
|
|
|
|
|
|
| 86 |
caption = generate_image_caption(image_data)
|
| 87 |
if caption.startswith("Error"):
|
| 88 |
raise HTTPException(status_code=500, detail=caption)
|
| 89 |
|
|
|
|
| 90 |
ai_response = next(run(caption, [], system_prompt))
|
| 91 |
|
| 92 |
# Define a function to translate the story to the target language
|
| 93 |
-
def translate_story(ai_response, target_language):
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# Add translation task to background tasks
|
| 98 |
background_tasks.add_task(translate_story, ai_response, target_language)
|
| 99 |
|
|
|
|
| 100 |
return {"status": "Generating story and translating to the target language. Please wait."}
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 105 |
|
|
|
|
| 106 |
@app.get("/")
|
| 107 |
def index() -> FileResponse:
|
| 108 |
return FileResponse(path="/app/static/index.html", media_type="text/html")
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import BackgroundTasks, FastAPI, File, UploadFile, Form, HTTPException
|
|
|
|
|
|
|
|
|
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
+
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
import os
|
| 5 |
import base64
|
| 6 |
+
import requests
|
| 7 |
+
import random
|
| 8 |
from typing import Iterator
|
|
|
|
| 9 |
from text_generation import Client
|
| 10 |
from deep_translator import GoogleTranslator
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
+
# Initialize Hugging Face API client
|
| 15 |
model_id = 'codellama/CodeLlama-34b-Instruct-hf'
|
|
|
|
| 16 |
API_URL = "https://api-inference.huggingface.co/models/" + model_id
|
| 17 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 18 |
+
client = Client(API_URL, headers={"Authorization": f"Bearer {HF_TOKEN}"})
|
| 19 |
+
|
| 20 |
+
# Initialize Google Translator
|
| 21 |
+
translator = GoogleTranslator()
|
| 22 |
|
| 23 |
+
# Constants for end-of-sequence strings
|
|
|
|
|
|
|
|
|
|
| 24 |
EOS_STRING = "</s>"
|
| 25 |
EOT_STRING = "<EOT>"
|
| 26 |
|
| 27 |
+
# Function to generate a prompt for the text generation model
|
| 28 |
+
def get_prompt(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> str:
|
|
|
|
| 29 |
texts = [f'<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n']
|
| 30 |
do_strip = False
|
| 31 |
for user_input, response in chat_history:
|
|
|
|
| 36 |
texts.append(f'{message} [/INST]')
|
| 37 |
return ''.join(texts)
|
| 38 |
|
| 39 |
+
# Function to generate a story based on an image caption
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def generate_image_caption(image_data):
|
| 41 |
image_base64 = base64.b64encode(image_data).decode('utf-8')
|
| 42 |
payload = {"data": ["data:image/jpeg;base64," + image_base64]}
|
|
|
|
| 47 |
else:
|
| 48 |
return "Error: Unable to generate caption"
|
| 49 |
|
| 50 |
+
# Function to generate a story and translate it to the target language
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
@app.post("/generate-story/")
|
| 52 |
async def generate_story(image_file: UploadFile = File(...), target_language: str = Form(...), background_tasks: BackgroundTasks = BackgroundTasks()):
|
| 53 |
+
# Read image data from the uploaded file
|
| 54 |
image_data = await image_file.read()
|
| 55 |
+
|
| 56 |
+
# Generate a system prompt for the story
|
| 57 |
system_prompt = f"write an attractive story in 300 words about {random.choice(['an adventurous journey', 'a mysterious encounter', 'a heroic quest'])}"
|
| 58 |
+
|
| 59 |
+
# Generate a caption for the uploaded image
|
| 60 |
caption = generate_image_caption(image_data)
|
| 61 |
if caption.startswith("Error"):
|
| 62 |
raise HTTPException(status_code=500, detail=caption)
|
| 63 |
|
| 64 |
+
# Generate a response using the text generation model
|
| 65 |
ai_response = next(run(caption, [], system_prompt))
|
| 66 |
|
| 67 |
# Define a function to translate the story to the target language
|
| 68 |
+
async def translate_story(ai_response, target_language):
|
| 69 |
+
try:
|
| 70 |
+
translated_story = translator.translate(ai_response, target_language=target_language)
|
| 71 |
+
return translated_story
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return f"Translation error: {str(e)}"
|
| 74 |
|
| 75 |
# Add translation task to background tasks
|
| 76 |
background_tasks.add_task(translate_story, ai_response, target_language)
|
| 77 |
|
| 78 |
+
# Return a response indicating that the story is being generated and translated
|
| 79 |
return {"status": "Generating story and translating to the target language. Please wait."}
|
| 80 |
|
| 81 |
+
# Mount static files
|
|
|
|
| 82 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 83 |
|
| 84 |
+
# Serve index.html as the default endpoint
|
| 85 |
@app.get("/")
|
| 86 |
def index() -> FileResponse:
|
| 87 |
return FileResponse(path="/app/static/index.html", media_type="text/html")
|
| 88 |
|
| 89 |
+
# Function to run the text generation model
|
| 90 |
+
def run(message: str, chat_history: list[tuple[str, str]], system_prompt: str, max_new_tokens: int = 1024, temperature: float = 0.1, top_p: float = 0.9, top_k: int = 50) -> Iterator[str]:
|
| 91 |
+
prompt = get_prompt(message, chat_history, system_prompt)
|
| 92 |
+
generate_kwargs = dict(
|
| 93 |
+
max_new_tokens=max_new_tokens,
|
| 94 |
+
do_sample=True,
|
| 95 |
+
top_p=top_p,
|
| 96 |
+
top_k=top_k,
|
| 97 |
+
temperature=temperature,
|
| 98 |
+
)
|
| 99 |
+
stream = client.generate_stream(prompt, **generate_kwargs)
|
| 100 |
+
output = ""
|
| 101 |
+
for response in stream:
|
| 102 |
+
if any([end_token in response.token.text for end_token in [EOS_STRING, EOT_STRING]]):
|
| 103 |
+
yield output
|
| 104 |
+
output = ""
|
| 105 |
+
else:
|
| 106 |
+
output += response.token.text
|