Delete untitled0.py
Browse files- untitled0.py +0 -91
untitled0.py
DELETED
|
@@ -1,91 +0,0 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
"""Untitled0.ipynb
|
| 3 |
-
|
| 4 |
-
Automatically generated by Colaboratory.
|
| 5 |
-
|
| 6 |
-
Original file is located at
|
| 7 |
-
https://colab.research.google.com/drive/1txpcrNvbvfMFFmdjhLFD7XbMCRdKKwVz
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
-
import os
|
| 11 |
-
import random
|
| 12 |
-
import time
|
| 13 |
-
import requests
|
| 14 |
-
from PIL import Image
|
| 15 |
-
import openai
|
| 16 |
-
from dotenv import load_dotenv, find_dotenv
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
load_dotenv(find_dotenv())
|
| 20 |
-
|
| 21 |
-
openai.api_key = os.getenv('OPENAI_API_KEY') # Your GPT-3 API key
|
| 22 |
-
dalle_api_key = 'your-dalle-api-key' # Your DALL-E API key
|
| 23 |
-
|
| 24 |
-
def chat_completion(messages: list) -> str:
|
| 25 |
-
try:
|
| 26 |
-
completion = openai.ChatCompletion.create(
|
| 27 |
-
model='gpt-3.5-turbo',
|
| 28 |
-
messages=messages
|
| 29 |
-
)
|
| 30 |
-
return completion['choices'][0]['message']['content']
|
| 31 |
-
except:
|
| 32 |
-
return 'We are facing a technical issue at this moment.'
|
| 33 |
-
|
| 34 |
-
def dalle_image_generation(prompt: str) -> Image.Image:
|
| 35 |
-
try:
|
| 36 |
-
response = openai.Image.create(
|
| 37 |
-
model="image-alpha-001", # Replace with the appropriate DALL-E model name
|
| 38 |
-
prompt=prompt
|
| 39 |
-
)
|
| 40 |
-
image_url = response['url']
|
| 41 |
-
image_data = requests.get(image_url).content
|
| 42 |
-
image = Image.open(io.BytesIO(image_data))
|
| 43 |
-
return image
|
| 44 |
-
except:
|
| 45 |
-
return None
|
| 46 |
-
|
| 47 |
-
def generate_messages(messages: list, query: str) -> list:
|
| 48 |
-
formatted_messages = [
|
| 49 |
-
{
|
| 50 |
-
'role': 'system',
|
| 51 |
-
'content': 'You are a helpful Network Design assistant.'
|
| 52 |
-
}
|
| 53 |
-
]
|
| 54 |
-
for m in messages:
|
| 55 |
-
formatted_messages.append({
|
| 56 |
-
'role': 'user',
|
| 57 |
-
'content': m[0]
|
| 58 |
-
})
|
| 59 |
-
formatted_messages.append({
|
| 60 |
-
'role': 'assistant',
|
| 61 |
-
'content': m[1]
|
| 62 |
-
})
|
| 63 |
-
formatted_messages.append(
|
| 64 |
-
{
|
| 65 |
-
'role': 'user',
|
| 66 |
-
'content': query
|
| 67 |
-
}
|
| 68 |
-
)
|
| 69 |
-
return formatted_messages
|
| 70 |
-
|
| 71 |
-
def generate_response(query: str, chat_history: list) -> tuple:
|
| 72 |
-
messages = generate_messages(chat_history, query)
|
| 73 |
-
bot_message = chat_completion(messages)
|
| 74 |
-
|
| 75 |
-
# Check if the user wants an image
|
| 76 |
-
if "generate image" in query.lower():
|
| 77 |
-
# Assuming the user wants an image, generate an image using DALL-E
|
| 78 |
-
image_generation_prompt = "Generate an image of " + query
|
| 79 |
-
generated_image = dalle_image_generation(image_generation_prompt)
|
| 80 |
-
|
| 81 |
-
if generated_image:
|
| 82 |
-
# Do something with the generated image (e.g., save or display it)
|
| 83 |
-
generated_image.show()
|
| 84 |
-
# Save the image to a file
|
| 85 |
-
generated_image.save('generated_image.jpg')
|
| 86 |
-
|
| 87 |
-
time.sleep(random.randint(0, 5))
|
| 88 |
-
|
| 89 |
-
chat_history.append((query, bot_message))
|
| 90 |
-
return '', chat_history
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|