Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# Argument parser setup
|
| 6 |
+
parser = argparse.ArgumentParser(
|
| 7 |
+
description='Chatbot Interface with Customizable Parameters')
|
| 8 |
+
parser.add_argument('--model-url',
|
| 9 |
+
type=str,
|
| 10 |
+
default='http://134.28.190.100:8000/v1',
|
| 11 |
+
help='Model URL')
|
| 12 |
+
parser.add_argument('-m',
|
| 13 |
+
'--model',
|
| 14 |
+
type=str,
|
| 15 |
+
required=True,
|
| 16 |
+
default='TheBloke/Mistral-7B-Instruct-v0.2-AWQ',
|
| 17 |
+
help='Model name for the chatbot')
|
| 18 |
+
parser.add_argument('--temp',
|
| 19 |
+
type=float,
|
| 20 |
+
default=0.8,
|
| 21 |
+
help='Temperature for text generation')
|
| 22 |
+
parser.add_argument('--stop-token-ids',
|
| 23 |
+
type=str,
|
| 24 |
+
default='',
|
| 25 |
+
help='Comma-separated stop token IDs')
|
| 26 |
+
parser.add_argument("--host", type=str, default=None)
|
| 27 |
+
parser.add_argument("--port", type=int, default=8001)
|
| 28 |
+
|
| 29 |
+
# Parse the arguments
|
| 30 |
+
args = parser.parse_args()
|
| 31 |
+
|
| 32 |
+
# Set OpenAI's API key and API base to use vLLM's API server.
|
| 33 |
+
openai_api_key = "EMPTY"
|
| 34 |
+
openai_api_base = args.model_url
|
| 35 |
+
|
| 36 |
+
# Create an OpenAI client to interact with the API server
|
| 37 |
+
client = OpenAI(
|
| 38 |
+
api_key=openai_api_key,
|
| 39 |
+
base_url=openai_api_base,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# def add_document():
|
| 43 |
+
|
| 44 |
+
def predict(message, history):
|
| 45 |
+
# Convert chat history to OpenAI format
|
| 46 |
+
history_openai_format = []#[{
|
| 47 |
+
#"role": "system",
|
| 48 |
+
#"content": "You are a great ai assistant."
|
| 49 |
+
#}]
|
| 50 |
+
for human, assistant in history:
|
| 51 |
+
history_openai_format.append({"role": "user", "content": human})
|
| 52 |
+
history_openai_format.append({
|
| 53 |
+
"role": "assistant",
|
| 54 |
+
"content": assistant
|
| 55 |
+
})
|
| 56 |
+
history_openai_format.append({"role": "user", "content": message})
|
| 57 |
+
|
| 58 |
+
# Create a chat completion request and send it to the API server
|
| 59 |
+
stream = client.chat.completions.create(
|
| 60 |
+
model=args.model, # Model name to use
|
| 61 |
+
messages=history_openai_format, # Chat history
|
| 62 |
+
temperature=args.temp, # Temperature for text generation
|
| 63 |
+
stream=True, # Stream response
|
| 64 |
+
extra_body={
|
| 65 |
+
'repetition_penalty':
|
| 66 |
+
1,
|
| 67 |
+
'stop_token_ids': [
|
| 68 |
+
int(id.strip()) for id in args.stop_token_ids.split(',')
|
| 69 |
+
if id.strip()
|
| 70 |
+
] if args.stop_token_ids else []
|
| 71 |
+
})
|
| 72 |
+
|
| 73 |
+
# Read and return generated text from response stream
|
| 74 |
+
partial_message = ""
|
| 75 |
+
for chunk in stream:
|
| 76 |
+
partial_message += (chunk.choices[0].delta.content or "")
|
| 77 |
+
yield partial_message
|
| 78 |
+
|
| 79 |
+
with gr.Blocks(title="MethodAI 0.15", theme="Soft") as demo:
|
| 80 |
+
with gr.Row():
|
| 81 |
+
with gr.Column(scale=1):
|
| 82 |
+
gr.UploadButton("Click to upload PDFs",file_types=[".pdf"])
|
| 83 |
+
with gr.Column(scale=4):
|
| 84 |
+
# Create and launch a chat interface with Gradio
|
| 85 |
+
gr.ChatInterface(predict).queue()
|
| 86 |
+
# with demo:
|
| 87 |
+
# btn.upload(render_file, inputs=[btn], outputs=[show_img])
|
| 88 |
+
demo.launch(server_name=args.host, server_port=args.port, share=True)
|