-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
636 lines (475 loc) · 21.3 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# from dotenv import find_dotenv, load_dotenv
# from transformers import pipeline
# from langchain.prompts import PromptTemplate
# from langchain.chains import LLMChain
# from langchain.llms import OpenAI
# import requests
# import os
# import streamlit as st
# load_dotenv(find_dotenv())
# # Retrieve the Hugging Face API token from the environment
# HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
# ## img2text
# def img2text(filename):
# # image_to_text = pipeline("image-to-text", model= "")
# # text = image_to_text(url)[0]['generated_text']
# # print(text)
# # return text
# API_URL = "https://api-inference.huggingface.co/models/Salesforce/blip2-opt-2.7b"
# headers = {"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"}
# with open(filename, "rb") as f:
# data = f.read()
# response = requests.post(API_URL, headers=headers, data=data)
# print(response)
# print(response.json())
# print(response.content)
# #return output
# #return response.json()
# img2text("cats.jpg")
# # # llm
# # def generate_story(scenario):
# # template = """
# # You are a story teller;
# # You can generate a short story based on a single narrative,
# # the story should be no more than 20 words;
# # CONTEXT: {scenario}
# # STORY:
# # """
# # prompt = PromptTemplate(template=template, input_variables=["scenario"])
# # story_llm = LLMChain(llm=OpenAI(
# # model_name="gpt-3.5-turbo", temperature=1),
# # prompt=prompt, verbose=True
# # )
# # story = story_llm.predict(scenario=scenario)
# # print(story)
# # return story
# # # text to speech
# # def text2speech(message):
# # API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
# # headers = {"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"}
# # payloads = {
# # "inputs": message
# # }
# # response = requests.post(API_URL, headers=headers, json=payloads)
# # with open('audio.flac', 'wb') as file:
# # file.write(response.content)
# # def main():
# # st.set_page_config(page_title="img 2 audio story", page_icon="🎈")
# # st.header("Turn img into audio story")
# # uploaded_file = st.file_uploader("Choose an image...", type=
# # "jpg")
# # if uploaded_file is not None:
# # print(uploaded_file)
# # bytes_data = uploaded_file.getvalue()
# # with open(uploaded_file.name, "wb") as file:
# # file.write(bytes_data)
# # st.image(uploaded_file, caption="Uploaded Image.",
# # use_column_width=True)
# # scenario = img2text(uploaded_file.name)
# # story = generate_story(scenario)
# # text2speech(story)
# # with st.expander("scenario"):
# # st.write(scenario)
# # with st.expander("story"):
# # st.write(story)
# # st.audio("audio.flac")
# # if __name__ == '__main__':
# # main()
import requests
import streamlit as st
import os
from openai import OpenAI
from dotenv import find_dotenv, load_dotenv
# Load environment variables
load_dotenv(find_dotenv())
# Retrieve API keys from .env
IMGUR_CLIENT_ID = os.getenv("IMGUR_CLIENT_ID")
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
def upload_to_imgur(image):
"""Upload an image to Imgur and return the URL."""
headers = {"Authorization": f"Client-ID {IMGUR_CLIENT_ID}"}
url = "https://api.imgur.com/3/image"
# Send the image to Imgur
response = requests.post(url, headers=headers, files={"image": image})
if response.status_code == 200:
# Get the URL of the uploaded image
image_url = response.json()["data"]["link"]
return image_url
else:
st.error("Failed to upload image")
return None
def generate_image_text(image_url):
"""Use OpenRouter's API to process the image and return the description."""
if not OPENROUTER_API_KEY:
st.error("API Key not found. Make sure OPENROUTER_API_KEY is set in your environment.")
return
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=OPENROUTER_API_KEY
)
# Make the API request to generate text from the uploaded image URL
completion = client.chat.completions.create(
extra_headers={
"HTTP-Referer": os.getenv("YOUR_SITE_URL"), # Optional, for rankings on OpenRouter
"X-Title": os.getenv("YOUR_APP_NAME"), # Optional, for app title in OpenRouter rankings
},
model="qwen/qwen-2-vl-7b-instruct:free",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What's in this image?"
},
{
"type": "image_url",
"image_url": {
"url": image_url
}
}
]
}
]
)
# Return the response from OpenRouter
return completion.choices[0].message.content
# text = generate_image_text("https://imgur.com/ZzNtnuG"+".jpeg")
# print(text)
# Streamlit GUI for image upload and processing
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
if uploaded_file is not None:
# Upload the image to Imgur and get the URL
image_url = upload_to_imgur(uploaded_file)
if image_url:
st.write(f"Image URL: {image_url}")
st.image(image_url, caption="Uploaded Image", use_column_width=True)
# Generate text from the uploaded image
result = generate_image_text(image_url)
if result:
st.write("Image Description:", result)
# from langchain.chat_models import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.chains import LLMChain
# from os import getenv
# from dotenv import find_dotenv, load_dotenv
# # Load environment variables
# load_dotenv(find_dotenv())
# def generate_story(scenario):
# template = """
# You are a story teller;
# You can generate a short horror story based on a single narrative,
# the story should be no more than 1500 words;
# CONTEXT: {scenario}
# STORY:
# """
# prompt = PromptTemplate(template=template, input_variables=["scenario"])
# llm = ChatOpenAI(
# openai_api_key=getenv("OPENROUTER_API_KEY"),
# openai_api_base=getenv("OPENROUTER_BASE_URL"),
# model_name="mattshumer/reflection-70b:free",
# )
# story_llm = LLMChain(llm=llm,
# prompt=prompt, verbose=True
# )
# story = story_llm.predict(scenario=scenario)
# print(story)
# return story
# # Example usage
# scenario = "A knight fighting a dragon to save a princess"
# generate_story(scenario)
# from openai import OpenAI
# from os import getenv
# # Initialize the OpenAI client with your API key
# api_key = getenv("OPENROUTER_API_KEY")
# if not api_key:
# raise ValueError("API Key not found. Make sure OPENROUTER_API_KEY is set in your environment.")
# # gets API Key from environment variable OPENAI_API_KEY
# client = OpenAI(
# base_url="https://openrouter.ai/api/v1",
# api_key=api_key,
# )
# def generate_story(scenario):
# response = client.chat.completions.create(
# model="mattshumer/reflection-70b:free",
# messages=[
# {
# "role": "user",
# "content": [
# {"type": "text", "text": "Generate a short story based on this narrative:"},
# {"type": "text", "text": scenario}
# ]
# }
# ]
# )
# story = response.choices[0].message.content
# print(story)
# return story
# # Example usage
# scenario = "A knight fighting a dragon to save a princess"
# generate_story(scenario)
# import requests
# from dotenv import find_dotenv, load_dotenv
# import os
# load_dotenv(find_dotenv())
# HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
# import time
# def text2speech(message, max_retries=5, retry_delay=10):
# API_URL = "https://api-inference.huggingface.co/models/facebook/fastspeech2-en-ljspeech"
# headers = {"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"}
# payloads = {"inputs": message}
# retries = 0
# while retries < max_retries:
# response = requests.post(API_URL, headers=headers, json=payloads)
# if response.status_code == 200:
# content_type = response.headers.get('Content-Type')
# print(f"Content-Type: {content_type}")
# if 'audio/flac' in content_type:
# with open('audio.flac', 'wb') as file:
# file.write(response.content)
# print("Audio file saved successfully.")
# break
# elif response.status_code == 503:
# # Model is loading, wait for the estimated time
# response_data = response.json()
# estimated_time = response_data.get("estimated_time", retry_delay)
# print(f"Model is loading. Retrying in {estimated_time} seconds...")
# time.sleep(estimated_time)
# retries += 1
# elif response.status_code == 500 and 'Model too busy' in response.text:
# print(f"Model too busy. Retrying in {retry_delay} seconds... ({retries+1}/{max_retries})")
# retries += 1
# time.sleep(retry_delay)
# else:
# print(f"Error: {response.status_code} - {response.text}")
# break
# else:
# print("Max retries reached. Unable to process the request.")
# Example usage
# text2speech("Hello man be okay")
# def text2speech(message):
# API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
# headers = {"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"}
# payloads = {
# "inputs": message
# }
# response = requests.post(API_URL, headers=headers, json=payloads)
# with open('audio.flac', 'wb') as file:
# file.write(response.content)
# print("Audio file saved successfully.")
# story = """
# In the land of Eridoria, a beautiful princess named Sophia was kidnapped by a fierce dragon named Tharros. The dragon took Sophia deep into its lair, a cave system hidden in the mountains. The king, desperate to save his daughter, called upon his bravest knight, Sir Edward.
# Sir Edward set off immediately to rescue Sophia. As he approached the dragon's lair, he could feel the ground shaking beneath his feet from Tharros's powerful movements. He drew his sword and charged into battle, ready to face whatever dangers lay ahead.
# Tharros, sensing the knight's presence, emerged from the shadows. Its scales glinted in the dim light of the cave, and its eyes burned with fierce intensity. Sir Edward stood tall, his armor gleaming in the faint light.
# The battle raged on, with Sir Edward dodging the dragon's flames and striking at its scales. But Tharros was relentless, its claws swiping at the knight with precision. Sir Edward parried each blow, his sword clanging against the dragon's claws.
# Just as the knight thought he had the upper hand, Tharros unleashed a powerful blast of fire. Sir Edward raised his shield, but the flames were too intense. He stumbled backward, his armor scorched and smoldering.
# Sophia, seeing her rescuer in danger, found the strength to break free from her chains. She rushed to Sir Edward's side, using her knowledge of the cave system to guide him to a hidden chamber filled with reflective crystals. The knight used these crystals to redirect the dragon's fire back at itself, weakening Tharros significantly.
# With the dragon weakened, Sir Edward launched a final attack. Sophia, filled with newfound courage, stood by his side, offering words of encouragement as she watched the knight fight for her life. Together, they managed to defeat Tharros and free the kingdom from its terror.
# As they made their way back to the castle, Sophia realized that she had fallen in love with Sir Edward, not just as her rescuer, but as the man who had shown her her own strength and bravery. The kingdom celebrated their victory, and the king, grateful for his daughter's safe return, asked Sir Edward to be his son-in-law. The knight, honored by the request, accepted, and the two were married in a grand ceremony, marking the beginning of a new era of peace and prosperity for the kingdom of Eridoria.
# """
# text2speech(story)
################# FULL WORKING CODE #######################
# from dotenv import find_dotenv, load_dotenv
# from langchain.chat_models import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.chains import LLMChain
# from openai import OpenAI
# import requests
# import os
# import time
# import streamlit as st
# load_dotenv(find_dotenv())
# # Retrieve API keys from .env
# IMGUR_CLIENT_ID = os.getenv("IMGUR_CLIENT_ID")
# OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
# HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
# def upload_to_imgur(image):
# """Upload an image to Imgur and return the URL."""
# headers = {"Authorization": f"Client-ID {IMGUR_CLIENT_ID}"}
# url = "https://api.imgur.com/3/image"
# # Send the image to Imgur
# response = requests.post(url, headers=headers, files={"image": image})
# if response.status_code == 200:
# # Get the URL of the uploaded image
# image_url = response.json()["data"]["link"]
# return image_url
# else:
# st.error("Failed to upload image")
# return None
# ## img2text
# def img2text(image_url):
# """Use OpenRouter's API to process the image and return the description."""
# if not OPENROUTER_API_KEY:
# print("API Key not found. Make sure OPENROUTER_API_KEY is set in your environment.")
# return
# client = OpenAI(
# base_url="https://openrouter.ai/api/v1",
# api_key=OPENROUTER_API_KEY
# )
# # Make the API request to generate text from the uploaded image URL
# completion = client.chat.completions.create(
# extra_headers={
# "HTTP-Referer": os.getenv("YOUR_SITE_URL"), # Optional, for rankings on OpenRouter
# "X-Title": os.getenv("YOUR_APP_NAME"), # Optional, for app title in OpenRouter rankings
# },
# model="qwen/qwen-2-vl-7b-instruct:free",
# messages=[
# {
# "role": "user",
# "content": [
# {
# "type": "text",
# "text": "What's in this image?"
# },
# {
# "type": "image_url",
# "image_url": {
# "url": image_url
# }
# }
# ]
# }
# ]
# )
# # Return the response from OpenRouter
# return completion.choices[0].message.content
# # llm
# def generate_story(scenario):
# template = """
# You are a story teller;
# You can generate a short horror story based on a single narrative,
# the story should be no more than 2000 words;
# CONTEXT: {scenario}
# STORY:
# """
# prompt = PromptTemplate(template=template, input_variables=["scenario"])
# llm = ChatOpenAI(
# openai_api_key=os.getenv("OPENROUTER_API_KEY"),
# openai_api_base=os.getenv("OPENROUTER_BASE_URL"),
# model_name="mattshumer/reflection-70b:free",
# )
# story_llm = LLMChain(llm=llm,
# prompt=prompt, verbose=True
# )
# story = story_llm.predict(scenario=scenario)
# return story
# # text to speech
# def text2speech(message, max_retries=5, retry_delay=10):
# API_URL = "https://api-inference.huggingface.co/models/facebook/fastspeech2-en-ljspeech"
# headers = {"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"}
# payloads = {"inputs": message}
# retries = 0
# while retries < max_retries:
# response = requests.post(API_URL, headers=headers, json=payloads)
# if response.status_code == 200:
# content_type = response.headers.get('Content-Type')
# print(f"Content-Type: {content_type}")
# if 'audio/flac' in content_type:
# with open('audio.flac', 'wb') as file:
# file.write(response.content)
# print("Audio file saved successfully.")
# break
# elif response.status_code == 503:
# # Model is loading, wait for the estimated time
# response_data = response.json()
# estimated_time = response_data.get("estimated_time", retry_delay)
# print(f"Model is loading. Retrying in {estimated_time} seconds...")
# time.sleep(estimated_time)
# retries += 1
# elif response.status_code == 500 and 'Model too busy' in response.text:
# print(f"Model too busy. Retrying in {retry_delay} seconds... ({retries+1}/{max_retries})")
# retries += 1
# time.sleep(retry_delay)
# else:
# print(f"Error: {response.status_code} - {response.text}")
# break
# else:
# print("Max retries reached. Unable to process the request.")
# # streamlit gui
# def main():
# st.set_page_config(page_title="Orro Stories", page_icon="🎈")
# st.header("Orro Stories")
# st.write("Generate audio Horror story based on the image you upload.")
# uploaded_file = st.file_uploader("Choose an image...", type=
# ["png", "jpg", "jpeg"])
# if uploaded_file is not None:
# # Upload the image to Imgur and get the URL
# image_url = upload_to_imgur(uploaded_file)
# if image_url:
# print(f"Image URL: {image_url}")
# st.image(image_url, caption="Uploaded Image", use_column_width=True)
# # Generate text from the uploaded image
# scenario = img2text(image_url)
# story = generate_story(scenario)
# text2speech(story)
# with st.expander("scenario"):
# st.write("Image Description:", scenario)
# with st.expander("story"):
# st.write(story)
# st.audio("audio.flac")
# if __name__ == '__main__':
# main()
# from dotenv import find_dotenv, load_dotenv
# import os
# import requests
# import cloudinary
# import cloudinary.uploader
# import io
# import time
# load_dotenv(find_dotenv())
# # Retrieve API keys from .env
# HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
# CLOUDINARY_NAME = os.getenv("CLOUDINARY_NAME")
# CLOUDINARY_API_KEY = os.getenv("CLOUDINARY_API_KEY")
# CLOUDINARY_API_SECRET = os.getenv("CLOUDINARY_API_SECRET")
# def text2speech(message, max_retries=5, retry_delay=10):
# # Initialize Cloudinary
# cloudinary.config(
# cloud_name=CLOUDINARY_NAME,
# api_key=CLOUDINARY_API_KEY,
# api_secret=CLOUDINARY_API_SECRET
# )
# API_URL = "https://api-inference.huggingface.co/models/facebook/fastspeech2-en-ljspeech"
# headers = {"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"}
# payloads = {"inputs": message}
# retries = 0
# while retries < max_retries:
# response = requests.post(API_URL, headers=headers, json=payloads)
# if response.status_code == 200:
# content_type = response.headers.get('Content-Type')
# print(f"Content-Type: {content_type}")
# if 'audio/flac' in content_type:
# # Prepare the audio file for uploading
# audio_data = io.BytesIO(response.content)
# audio_data.seek(0) # Reset file pointer to the start
# # Upload to Cloudinary with the proper resource type
# upload_response = cloudinary.uploader.upload(
# audio_data,
# resource_type='raw', # 'raw' should be used for generic files like audio
# public_id="generated_audio",
# format="flac" # Ensuring it's uploaded as an audio file
# )
# # Retrieve the URL for the uploaded audio
# audio_url = upload_response.get('url')
# print("Audio file uploaded successfully.")
# return audio_url
# elif response.status_code == 503:
# # Model is loading, wait for the estimated time
# response_data = response.json()
# estimated_time = response_data.get("estimated_time", retry_delay)
# st.info(f"Model is loading. Retrying in {estimated_time} seconds...")
# time.sleep(estimated_time)
# retries += 1
# elif response.status_code == 500 and 'Model too busy' in response.text:
# st.info(f"Model too busy. Retrying in {retry_delay} seconds... ({retries+1}/{max_retries})")
# retries += 1
# time.sleep(retry_delay)
# else:
# st.error(f"Error: {response.status_code} - {response.text}")
# break
# else:
# st.error("Max retries reached. Unable to process the request.")
# return None
# # Example usage
# audio_url = text2speech("Hello, world!")
# print("Audio uploaded to:", audio_url)