Spaces:
Running
Running
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
import nltk
|
5 |
+
import random
|
6 |
+
from openai import OpenAI
|
7 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
8 |
+
from diffusers import StableDiffusionPipeline
|
9 |
+
from ultralytics import YOLO
|
10 |
+
from gtts import gTTS
|
11 |
+
from PIL import Image
|
12 |
+
import numpy as np
|
13 |
+
|
14 |
+
# Βεβαιωθείτε ότι το API Key υπάρχει
|
15 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
16 |
+
if not api_key:
|
17 |
+
raise ValueError("⚠️ OpenAI API Key is missing! Add it as a Secret in Hugging Face Spaces.")
|
18 |
+
|
19 |
+
# OpenAI Client
|
20 |
+
client = OpenAI(api_key=api_key)
|
21 |
+
|
22 |
+
# Φόρτωση μοντέλων
|
23 |
+
print("Loading models...")
|
24 |
+
yolo_model = YOLO("yolov8s.pt") # Μοντέλο Object Detection
|
25 |
+
text_generation = pipeline("text-generation", model="gpt2")
|
26 |
+
stable_diffusion = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
|
27 |
+
nltk.download("punkt")
|
28 |
+
|
29 |
+
def generate_story(prompt):
|
30 |
+
response = client.completions.create(
|
31 |
+
model="gpt-4o",
|
32 |
+
prompt=prompt,
|
33 |
+
max_tokens=300
|
34 |
+
)
|
35 |
+
return response.choices[0].text.strip()
|
36 |
+
|
37 |
+
def detect_objects(image):
|
38 |
+
results = yolo_model(image)
|
39 |
+
return results[0].plot()
|
40 |
+
|
41 |
+
def generate_image(prompt):
|
42 |
+
image = stable_diffusion(prompt).images[0]
|
43 |
+
return image
|
44 |
+
|
45 |
+
def text_to_speech(text):
|
46 |
+
tts = gTTS(text=text, lang="en")
|
47 |
+
tts.save("output.mp3")
|
48 |
+
return "output.mp3"
|
49 |
+
|
50 |
+
demo = gr.Interface(
|
51 |
+
fn={
|
52 |
+
"Generate Story": generate_story,
|
53 |
+
"Detect Objects": detect_objects,
|
54 |
+
"Generate Image": generate_image,
|
55 |
+
"Text to Speech": text_to_speech,
|
56 |
+
},
|
57 |
+
inputs={
|
58 |
+
"Generate Story": gr.Textbox(placeholder="Write a story prompt..."),
|
59 |
+
"Detect Objects": gr.Image(type="numpy"),
|
60 |
+
"Generate Image": gr.Textbox(placeholder="Describe an image..."),
|
61 |
+
"Text to Speech": gr.Textbox(placeholder="Enter text to convert to speech...")
|
62 |
+
},
|
63 |
+
outputs={
|
64 |
+
"Generate Story": "text",
|
65 |
+
"Detect Objects": "image",
|
66 |
+
"Generate Image": "image",
|
67 |
+
"Text to Speech": "audio"
|
68 |
+
},
|
69 |
+
title="AI-Powered Storytelling Assistant",
|
70 |
+
description="An AI assistant combining NLP, Object Detection, Image Generation, and TTS!"
|
71 |
+
)
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
demo.launch()
|
75 |
+
|
76 |
+
|