PoliticalLLM / app.py
jost's picture
Update app.py
b908c2d verified
raw
history blame
1.58 kB
from openai import OpenAI
import gradio as gr
import time
anyscale_base_url = "https://api.endpoints.anyscale.com/v1"
def predict(api_key, user_input):
prompt = f"""[INST] {user_input} [/INST]"""
client = OpenAI(base_url=anyscale_base_url, api_key=api_key)
completion = client.completions.create(
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
prompt=prompt,
temperature=0.7,
max_tokens=100)
response = completion.choices[0].text
# Simulate streaming by splitting the response and yielding parts with delays
words = response.split()
for i in range(0, len(words), 5): # Adjust the step to control chunk sizes
yield ' '.join(words[i:i+5])
time.sleep(1) # Adjust the sleep time to control the streaming speeded
#return response
def main():
description = "This is a simple interface to interact with OpenAI’s Chat Completion API. Please enter your API key and your message."
with gr.Blocks() as demo:
with gr.Row():
api_key_input = gr.Textbox(label="API Key", placeholder="Enter your API key here", show_label=True, type="password")
user_input = gr.Textbox(label="Your Message", placeholder="Enter your message here")
submit_btn = gr.Button("Submit")
output = gr.Textbox(label="LLM Response", live=True)
#output = gr.Textbox(label="LLM Response")
submit_btn.click(fn=predict, inputs=[api_key_input, user_input], outputs=output)
demo.launch()
if __name__ == "__main__":
main()