Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
|
|
|
|
|
| 1 |
from transformers import pipeline, Conversation
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
# Load environment variables from the .env file de forma local
|
|
@@ -10,50 +12,22 @@ import base64
|
|
| 10 |
with open("Iso_Logotipo_Ceibal.png", "rb") as image_file:
|
| 11 |
encoded_image = base64.b64encode(image_file.read()).decode()
|
| 12 |
|
| 13 |
-
# chatbot = pipeline(model="microsoft/DialoGPT-medium")
|
| 14 |
-
# conversation = Conversation("Hi")
|
| 15 |
-
# response = chatbot(conversation)
|
| 16 |
-
# #conversation.mark_processed()
|
| 17 |
-
# #conversation.append_response(response)
|
| 18 |
-
# conversation.add_user_input("How old are you?")
|
| 19 |
-
|
| 20 |
-
# conversation2 = chatbot(conversation)
|
| 21 |
-
# print(conversation2)
|
| 22 |
-
|
| 23 |
-
# def respond(text, conversation):
|
| 24 |
-
# chatbot = pipeline(model="microsoft/DialoGPT-medium")
|
| 25 |
-
|
| 26 |
-
# if len(conversation)==0:
|
| 27 |
-
# conversation = Conversation(text)
|
| 28 |
-
# conversation = chatbot(conversation)
|
| 29 |
-
# print(conversation.iter_texts())
|
| 30 |
-
# # test = []
|
| 31 |
-
# # for user,text in conversation.iter_texts():
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# return text, conversation.iter_texts()
|
| 35 |
-
# else:
|
| 36 |
-
# conversation.add_user_input(text)
|
| 37 |
-
# conversation = chatbot(conversation)
|
| 38 |
-
# return text, conversation.iter_texts()
|
| 39 |
-
|
| 40 |
-
import os
|
| 41 |
-
import openai
|
| 42 |
|
| 43 |
openai.api_key = os.environ['OPENAI_API_KEY']
|
| 44 |
|
| 45 |
def clear_chat(message, chat_history):
|
| 46 |
return "", []
|
| 47 |
|
| 48 |
-
def add_new_message(message,chat_history):
|
| 49 |
new_chat = []
|
|
|
|
| 50 |
for turn in chat_history:
|
| 51 |
user, bot = turn
|
| 52 |
new_chat.append({"role": "user", "content": user})
|
| 53 |
new_chat.append({"role": "assistant","content":bot})
|
| 54 |
new_chat.append({"role": "user","content":message})
|
| 55 |
return new_chat
|
| 56 |
-
|
| 57 |
def respond(message, chat_history):
|
| 58 |
prompt = add_new_message(message, chat_history)
|
| 59 |
# stream = client.generate_stream(prompt,
|
|
@@ -67,13 +41,14 @@ def respond(message, chat_history):
|
|
| 67 |
messages= prompt,
|
| 68 |
temperature=0.5,
|
| 69 |
max_tokens=1000,
|
| 70 |
-
stream
|
| 71 |
)#.choices[0].message.content
|
| 72 |
-
#
|
| 73 |
|
| 74 |
token_counter = 0
|
| 75 |
partial_words = ""
|
| 76 |
|
|
|
|
| 77 |
for chunk in response:
|
| 78 |
chunk_message = chunk['choices'][0]['delta']
|
| 79 |
if(len(chat_history))<1:
|
|
@@ -91,7 +66,6 @@ def respond(message, chat_history):
|
|
| 91 |
chat_history[-1] =([message,partial_words])
|
| 92 |
yield "",chat_history
|
| 93 |
|
| 94 |
-
# return "",chat_history
|
| 95 |
|
| 96 |
with gr.Blocks() as demo:
|
| 97 |
gr.Markdown("""
|
|
@@ -114,11 +88,10 @@ with gr.Blocks() as demo:
|
|
| 114 |
with gr.Column(scale=1):
|
| 115 |
btn = gr.Button("Enviar")
|
| 116 |
clear = gr.ClearButton(components=[msg, chatbot], value="Borrar chat")
|
| 117 |
-
|
| 118 |
btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 119 |
-
msg.submit(respond, inputs=[msg,
|
| 120 |
clear.click(clear_chat,inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
demo.queue(concurrency_count=4)
|
| 124 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
from transformers import pipeline, Conversation
|
| 4 |
import gradio as gr
|
| 5 |
+
import json
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
# Load environment variables from the .env file de forma local
|
|
|
|
| 12 |
with open("Iso_Logotipo_Ceibal.png", "rb") as image_file:
|
| 13 |
encoded_image = base64.b64encode(image_file.read()).decode()
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
openai.api_key = os.environ['OPENAI_API_KEY']
|
| 17 |
|
| 18 |
def clear_chat(message, chat_history):
|
| 19 |
return "", []
|
| 20 |
|
| 21 |
+
def add_new_message(message, chat_history):
|
| 22 |
new_chat = []
|
| 23 |
+
|
| 24 |
for turn in chat_history:
|
| 25 |
user, bot = turn
|
| 26 |
new_chat.append({"role": "user", "content": user})
|
| 27 |
new_chat.append({"role": "assistant","content":bot})
|
| 28 |
new_chat.append({"role": "user","content":message})
|
| 29 |
return new_chat
|
| 30 |
+
|
| 31 |
def respond(message, chat_history):
|
| 32 |
prompt = add_new_message(message, chat_history)
|
| 33 |
# stream = client.generate_stream(prompt,
|
|
|
|
| 41 |
messages= prompt,
|
| 42 |
temperature=0.5,
|
| 43 |
max_tokens=1000,
|
| 44 |
+
stream=True,
|
| 45 |
)#.choices[0].message.content
|
| 46 |
+
#chat_history.append((message, response))
|
| 47 |
|
| 48 |
token_counter = 0
|
| 49 |
partial_words = ""
|
| 50 |
|
| 51 |
+
counter=0
|
| 52 |
for chunk in response:
|
| 53 |
chunk_message = chunk['choices'][0]['delta']
|
| 54 |
if(len(chat_history))<1:
|
|
|
|
| 66 |
chat_history[-1] =([message,partial_words])
|
| 67 |
yield "",chat_history
|
| 68 |
|
|
|
|
| 69 |
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
gr.Markdown("""
|
|
|
|
| 88 |
with gr.Column(scale=1):
|
| 89 |
btn = gr.Button("Enviar")
|
| 90 |
clear = gr.ClearButton(components=[msg, chatbot], value="Borrar chat")
|
| 91 |
+
|
| 92 |
btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 93 |
+
msg.submit(respond, inputs=[msg ,chatbot], outputs=[msg, chatbot]) #Press enter to submit
|
| 94 |
clear.click(clear_chat,inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 95 |
+
|
| 96 |
+
demo.queue()
|
|
|
|
| 97 |
demo.launch()
|