Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import subprocess
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
# Start the Ollama server and wait for it to output the URL
|
| 7 |
+
subprocess.Popen(['bash', 'run_ollama.sh'])
|
| 8 |
+
time.sleep(10) # Adjust sleep time based on how long it takes Ollama to start
|
| 9 |
+
|
| 10 |
+
# Read the URL from the file
|
| 11 |
+
with open('ollama_url.txt', 'r') as file:
|
| 12 |
+
OLLAMA_URL = file.read().strip()
|
| 13 |
+
|
| 14 |
+
console_output = []
|
| 15 |
+
|
| 16 |
+
def call_ollama_api(prompt):
|
| 17 |
+
response = requests.post(
|
| 18 |
+
f'{OLLAMA_URL}/generate',
|
| 19 |
+
json={"prompt": prompt}
|
| 20 |
+
)
|
| 21 |
+
if response.status_code == 200:
|
| 22 |
+
result = response.json()
|
| 23 |
+
output = result.get('text', '')
|
| 24 |
+
console_output.append(f"Prompt: {prompt}\nResponse: {output}")
|
| 25 |
+
return output
|
| 26 |
+
else:
|
| 27 |
+
error_message = f"Error: {response.status_code} - {response.text}"
|
| 28 |
+
console_output.append(error_message)
|
| 29 |
+
return error_message
|
| 30 |
+
|
| 31 |
+
def chat(prompt):
|
| 32 |
+
output = call_ollama_api(prompt)
|
| 33 |
+
return output
|
| 34 |
+
|
| 35 |
+
def show_console_output():
|
| 36 |
+
return "\n\n".join(console_output)
|
| 37 |
+
|
| 38 |
+
iface = gr.Interface(
|
| 39 |
+
fn=chat,
|
| 40 |
+
inputs="text",
|
| 41 |
+
outputs="text",
|
| 42 |
+
title="Ollama Chat",
|
| 43 |
+
description="Chat with Ollama API and see the responses."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
console_iface = gr.Interface(
|
| 47 |
+
fn=show_console_output,
|
| 48 |
+
inputs=None,
|
| 49 |
+
outputs="text",
|
| 50 |
+
title="Console Output",
|
| 51 |
+
description="See the console outputs here."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
gr.TabbedInterface([iface, console_iface], ["Chat", "Console"]).launch()
|