Makhinur commited on
Commit
2be2394
·
verified ·
1 Parent(s): c9d0e24

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +63 -0
main.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from fastapi import FastAPI, File, UploadFile, Form, HTTPException
3
+ from fastapi.responses import FileResponse
4
+ from fastapi.staticfiles import StaticFiles
5
+ import requests
6
+ import base64
7
+ import os
8
+ import random
9
+ from huggingface_hub import InferenceClient
10
+ from deep_translator import GoogleTranslator
11
+
12
+ app = FastAPI()
13
+
14
+ client = InferenceClient(model="meta-llama/Meta-Llama-3-8B-Instruct")
15
+
16
+ def generate_image_caption(image_data: bytes) -> str:
17
+ image_base64 = base64.b64encode(image_data).decode('utf-8')
18
+ payload = {"data": ["data:image/jpeg;base64," + image_base64]}
19
+ response = requests.post("https://makhinur-image-to-text-salesforce-blip-image-cap-c0a9076.hf.space/run/predict", json=payload)
20
+ if response.status_code == 200:
21
+ caption = response.json()["data"][0]
22
+ return caption
23
+ else:
24
+ return "Error: Unable to generate caption"
25
+
26
+ def generate_story_text(caption, system_prompt, max_tokens, temperature, top_p):
27
+ messages = [
28
+ {"role": "system", "content": system_prompt},
29
+ {"role": "user", "content": caption}
30
+ ]
31
+
32
+ response = client.chat_completion(
33
+ messages=messages,
34
+ max_tokens=max_tokens,
35
+ temperature=temperature,
36
+ top_p=top_p
37
+ )
38
+
39
+ story_text = response.choices[0].message['content']
40
+ return story_text
41
+
42
+ @app.post("/generate-story/")
43
+ async def generate_story(image_file: UploadFile = File(...), language: str = Form(...)):
44
+ image_data = await image_file.read()
45
+ system_prompt = f"Write an attractive story in 300 words about {random.choice(['an adventurous journey', 'a mysterious encounter', 'a heroic quest', 'a magical adventure', 'a thrilling escape', 'an unexpected discovery', 'a dangerous mission', 'a romantic escapade', 'an epic battle', 'a journey into the unknown'])}"
46
+
47
+ caption = generate_image_caption(image_data)
48
+ if caption.startswith("Error"):
49
+ raise HTTPException(status_code=500, detail=caption)
50
+
51
+ ai_response = generate_story_text(
52
+ caption,
53
+ system_prompt,
54
+ max_tokens=512,
55
+ temperature=0.9,
56
+ top_p=0.95
57
+ )
58
+
59
+ if language.lower() != "english":
60
+ translator = GoogleTranslator(source='english', target=language)
61
+ ai_response = translator.translate(ai_response)
62
+
63
+ return {"story": ai_response}