Spaces:
Runtime error
Runtime error
File size: 1,691 Bytes
2a6132e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
from dotenv import load_dotenv
load_dotenv()
import os
import gradio as gr
import time
from openai import OpenAI
client = OpenAI()
directory = './documents'
file_ids = []
for filename in os.listdir(directory):
if filename.endswith(".pdf"): # Assuming you're only interested in PDF files
file_path = os.path.join(directory, filename)
with open(file_path, 'rb') as file:
uploaded_file = client.files.create(file=file, purpose='assistants')
file_ids.append(uploaded_file.id)
assistant = client.beta.assistants.create(
instructions="You are an expert consultant for SBIR grant proposals. Your retrievable files include SBIR proposal guidelines as well as several real SBIR grant applications you can use as examples to guide the user.",
model="gpt-4-1106-preview",
tools=[{"type": "retrieval"}],
file_ids=file_ids
)
thread = client.beta.threads.create()
def chat(prompt):
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=prompt
)
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
)
while run.status != "completed":
time.sleep(0.1)
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
return messages.data[0].content[0].text.value
# Define Gradio interface
iface = gr.Interface(
fn=chat,
inputs="text",
outputs=gr.Markdown(),
title="💬 SBIR Consultant",
description="🚀 An SBIR Grant Application Consultant"
)
iface.launch() |