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