Ollama-API / app.py
DeathDaDev's picture
Create app.py
5a6a740 verified
raw
history blame
1.43 kB
import gradio as gr
import requests
import subprocess
import time
# Start the Ollama server and wait for it to output the URL
subprocess.Popen(['bash', 'run_ollama.sh'])
time.sleep(10) # Adjust sleep time based on how long it takes Ollama to start
# Read the URL from the file
with open('ollama_url.txt', 'r') as file:
OLLAMA_URL = file.read().strip()
console_output = []
def call_ollama_api(prompt):
response = requests.post(
f'{OLLAMA_URL}/generate',
json={"prompt": prompt}
)
if response.status_code == 200:
result = response.json()
output = result.get('text', '')
console_output.append(f"Prompt: {prompt}\nResponse: {output}")
return output
else:
error_message = f"Error: {response.status_code} - {response.text}"
console_output.append(error_message)
return error_message
def chat(prompt):
output = call_ollama_api(prompt)
return output
def show_console_output():
return "\n\n".join(console_output)
iface = gr.Interface(
fn=chat,
inputs="text",
outputs="text",
title="Ollama Chat",
description="Chat with Ollama API and see the responses."
)
console_iface = gr.Interface(
fn=show_console_output,
inputs=None,
outputs="text",
title="Console Output",
description="See the console outputs here."
)
gr.TabbedInterface([iface, console_iface], ["Chat", "Console"]).launch()