Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,11 +26,52 @@ with gr.Blocks(theme=gr.themes.Monochrome(),css="footer{display:none !important}
|
|
| 26 |
|
| 27 |
Chatbot = gr.Chatbot(label="Anonymous User")
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
with gr.Row():
|
| 30 |
txt = gr.Textbox(
|
| 31 |
show_label=False,
|
| 32 |
placeholder = initial_message)
|
| 33 |
-
txt.submit(
|
| 34 |
txt.submit(None, None, txt, js="() => {''}")
|
| 35 |
|
| 36 |
chatblock.launch()
|
|
|
|
| 26 |
|
| 27 |
Chatbot = gr.Chatbot(label="Anonymous User")
|
| 28 |
|
| 29 |
+
g = Github(token)
|
| 30 |
+
|
| 31 |
+
def predict_prompt(input):
|
| 32 |
+
message_history.append({"role": "user", "content": input})
|
| 33 |
+
create_prompt = openai.ChatCompletion.create(
|
| 34 |
+
model = "gpt-4o",
|
| 35 |
+
messages = message_history
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Reply content for the chat bot
|
| 39 |
+
reply_prompt = create_prompt.choices[0].message.content
|
| 40 |
+
|
| 41 |
+
# Append answer as assistant reply to keep history of prompts
|
| 42 |
+
message_history.append({"role": "assistant", "content": reply_prompt})
|
| 43 |
+
response = [(message_history[i]["content"], message_history[i+1]["content"]) for i in range(1, len(message_history) - 1, 2)]
|
| 44 |
+
|
| 45 |
+
# Add token and connect to repository (again)
|
| 46 |
+
g = Github(token)
|
| 47 |
+
|
| 48 |
+
# Repository details
|
| 49 |
+
repo = g.get_user().get_repo("CATData")
|
| 50 |
+
file_path = filename
|
| 51 |
+
commit_message = "Updating file content"
|
| 52 |
+
|
| 53 |
+
# variable content store each message found in the message history and states the respective user. This is for the content to add in the file
|
| 54 |
+
content = ''
|
| 55 |
+
for i in range(1,len(message_history), 1):
|
| 56 |
+
if (i % 2 == 1) and (message_history[i]["role"] == "user"):
|
| 57 |
+
content += ('User: ' + message_history[i]["content"] + '\n')
|
| 58 |
+
if (i % 2 == 0) and (message_history[i]["role"] == "assistant"):
|
| 59 |
+
content += ('Bot: ' + message_history[i]["content"] + '\n')
|
| 60 |
+
else:
|
| 61 |
+
content += ''
|
| 62 |
+
|
| 63 |
+
#updating the repository with the entire message history
|
| 64 |
+
file = repo.get_contents(file_path)
|
| 65 |
+
repo.update_file(file.path, commit_message, content, file.sha)
|
| 66 |
+
|
| 67 |
+
#return response
|
| 68 |
+
return response
|
| 69 |
+
|
| 70 |
with gr.Row():
|
| 71 |
txt = gr.Textbox(
|
| 72 |
show_label=False,
|
| 73 |
placeholder = initial_message)
|
| 74 |
+
txt.submit(predict_prompt, txt, Chatbot)
|
| 75 |
txt.submit(None, None, txt, js="() => {''}")
|
| 76 |
|
| 77 |
chatblock.launch()
|