Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib as plt
|
| 4 |
+
import openai
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import time
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Importing required components directly from gradio
|
| 10 |
+
from gradio import components
|
| 11 |
+
|
| 12 |
+
SECRET_TOKEN = os.getenv('openai.api_key')
|
| 13 |
+
messages = [{"role": "system", "content": "You are a doctor"}]
|
| 14 |
+
|
| 15 |
+
def send_message(message):
|
| 16 |
+
response = openai.ChatCompletion.create(
|
| 17 |
+
model="gpt-3.5-turbo",
|
| 18 |
+
messages=message,
|
| 19 |
+
api_key=SECRET_TOKEN
|
| 20 |
+
)
|
| 21 |
+
ChatGPT_reply = response["choices"][0]["message"]["content"]
|
| 22 |
+
return ChatGPT_reply
|
| 23 |
+
|
| 24 |
+
def adaptive_truncate(message, token_limit):
|
| 25 |
+
# Truncate the message content to fit within the token limit
|
| 26 |
+
tokens = message["content"].split()
|
| 27 |
+
truncated_tokens = []
|
| 28 |
+
total_tokens = 0
|
| 29 |
+
for token in tokens:
|
| 30 |
+
total_tokens += len(token.split())
|
| 31 |
+
if total_tokens <= token_limit:
|
| 32 |
+
truncated_tokens.append(token)
|
| 33 |
+
else:
|
| 34 |
+
break
|
| 35 |
+
message["content"] = " ".join(truncated_tokens)
|
| 36 |
+
return message
|
| 37 |
+
|
| 38 |
+
def CustomChatGPT(enter_your_question):
|
| 39 |
+
# Adaptive token limit to leave some space for response
|
| 40 |
+
token_limit = 4000
|
| 41 |
+
|
| 42 |
+
# Initialize the messages list
|
| 43 |
+
messages = [{"role": "system", "content": "You are a doctor"}]
|
| 44 |
+
|
| 45 |
+
# Send user input as separate messages to the model
|
| 46 |
+
user_input_tokens = enter_your_question.split()
|
| 47 |
+
current_message = {"role": "user", "content": ""}
|
| 48 |
+
current_token_count = len("You are a doctor".split())
|
| 49 |
+
|
| 50 |
+
for token in user_input_tokens:
|
| 51 |
+
token_tokens = len(token.split())
|
| 52 |
+
if current_token_count + token_tokens <= token_limit:
|
| 53 |
+
current_message["content"] += token + " "
|
| 54 |
+
current_token_count += token_tokens
|
| 55 |
+
else:
|
| 56 |
+
# Truncate the current message to fit within token limit
|
| 57 |
+
current_message = adaptive_truncate(current_message, token_limit)
|
| 58 |
+
# Send the current message and get the response
|
| 59 |
+
reply = send_message(messages + [current_message])
|
| 60 |
+
messages.append({"role": "user", "content": " ".join(current_message["content"].split())})
|
| 61 |
+
messages[-1]["content"] = reply
|
| 62 |
+
# Start the next message with the remaining token
|
| 63 |
+
current_message = {"role": "user", "content": token + " "}
|
| 64 |
+
current_token_count = token_tokens
|
| 65 |
+
|
| 66 |
+
# Truncate and send the last message
|
| 67 |
+
current_message = adaptive_truncate(current_message, token_limit)
|
| 68 |
+
reply = send_message(messages + [current_message])
|
| 69 |
+
messages.append({"role": "user", "content": " ".join(current_message["content"].split())})
|
| 70 |
+
messages[-1]["content"] = reply
|
| 71 |
+
|
| 72 |
+
return reply
|
| 73 |
+
|
| 74 |
+
# Set up Gradio interface
|
| 75 |
+
iface = gr.Interface(
|
| 76 |
+
fn=CustomChatGPT,
|
| 77 |
+
inputs=components.Textbox(lines=1, label="Enter your question"),
|
| 78 |
+
outputs=components.Textbox(label="Doctor's advice"),
|
| 79 |
+
title="Doctor's desk. Ask any help related to health?",
|
| 80 |
+
examples=[
|
| 81 |
+
["What are the symptoms of flu?"],
|
| 82 |
+
["How can I prevent a cold?"],
|
| 83 |
+
["Is it safe to take antibiotics for a viral infection?"],
|
| 84 |
+
],
|
| 85 |
+
live=False, # Removed 'live' mode so that action is taken only after submit button is clicked
|
| 86 |
+
allow_flagging="never",
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
iface.launch(inline=False)
|