File size: 1,595 Bytes
5a6a740
 
 
 
a1e8dca
5a6a740
 
 
a1e8dca
 
 
 
 
5a6a740
 
a1e8dca
5a6a740
 
 
 
 
a1e8dca
 
 
 
 
 
5a6a740
 
 
 
a1e8dca
 
5a6a740
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1e8dca
5a6a740
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
import gradio as gr
import requests
import subprocess
import time
import os

# Start the Ollama server and wait for it to output the URL
subprocess.Popen(['bash', 'run_ollama.sh'])
ollama_url_file = 'ollama_url.txt'

# Wait for the server URL file to be created
while not os.path.exists(ollama_url_file):
    time.sleep(1)

# Read the URL from the file
with open(ollama_url_file, 'r') as file:
    OLLAMA_URL = file.read().strip()

console_output = []

def call_ollama_api(prompt):
    try:
        response = requests.post(
            f'{OLLAMA_URL}/generate',
            json={"prompt": prompt}
        )
        response.raise_for_status()  # Raise an exception for HTTP errors
        result = response.json()
        output = result.get('text', '')
        console_output.append(f"Prompt: {prompt}\nResponse: {output}")
        return output
    except requests.exceptions.RequestException as e:
        error_message = f"Error: {e}"
        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."
)

# Launch a tabbed interface
gr.TabbedInterface([iface, console_iface], ["Chat", "Console"]).launch()