AI Agent commited on
Commit
240219c
·
1 Parent(s): 9939bfd

Initial commit: Ollama server with Gradio interface

Browse files
Files changed (3) hide show
  1. README.md +44 -13
  2. app.py +155 -0
  3. requirements.txt +2 -0
README.md CHANGED
@@ -1,13 +1,44 @@
1
- ---
2
- title: Ollama Server Example
3
- emoji: 📉
4
- colorFrom: indigo
5
- colorTo: pink
6
- sdk: gradio
7
- sdk_version: 6.11.0
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Ollama Server Example 🦙
2
+
3
+ Prosty serwer Ollama na Hugging Face Spaces z interfejsem Gradio.
4
+
5
+ ## Funkcje
6
+
7
+ **Uruchamianie modeli** – Interakcja z modelami Ollama
8
+ **Pobieranie modeli** – Pobieranie nowych modeli z Ollama
9
+ **Lista modeli** – Przeglądanie zainstalowanych modeli
10
+ **Informacje o modelu** – Szczegółowe informacje o wybranym modelu
11
+
12
+ ## Użycie
13
+
14
+ 1. **Uruchom model** – Wprowadź nazwę modelu (np. `llama3`) i prompt
15
+ 2. **Pobierz model** – Wprowadź nazwę modelu do pobrania (np. `llama3`, `mistral`)
16
+ 3. **Lista modeli** – Kliknij "Odśwież Listę Modeli", aby zobaczyć dostępne modele
17
+ 4. **Informacje o modelu** – Wprowadź nazwę modelu, aby uzyskać szczegółowe informacje
18
+
19
+ ## Wymagania
20
+
21
+ - Ollama musi być zainstalowany i uruchomiony na instancji Space
22
+ - Zobacz: https://ollama.ai/
23
+
24
+ ## Jak to działa?
25
+
26
+
27
+ Ta aplikacja używa [Ollama](https://ollama.ai/) – narzędzia do uruchamiania dużych modeli językowych lokalnie. Aplikacja Gradio zapewnia prosty interfejs webowy do interakcji z modelami.
28
+
29
+ ## Uruchomienie lokalne
30
+
31
+
32
+ ```bash
33
+ # Zainstaluj wymagania
34
+ pip install -r requirements.txt
35
+
36
+ # Uruchom aplikację
37
+ python app.py
38
+ ```
39
+
40
+ ## Linki
41
+
42
+ - [Ollama](https://ollama.ai/)
43
+ - [Hugging Face Spaces](https://huggingface.co/spaces)
44
+ - [Gradio](https://gradio.app/)
app.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import subprocess
3
+ import os
4
+ import json
5
+ from typing import List, Dict, Any
6
+
7
+
8
+ def list_models() -> List[Dict[str, Any]]:
9
+ """List available Ollama models"""
10
+ try:
11
+ result = subprocess.run(
12
+ ["ollama", "list"],
13
+ capture_output=True,
14
+ text=True,
15
+ check=True
16
+ )
17
+ models = []
18
+ for line in result.stdout.splitlines():
19
+ if line.startswith("NAME"):
20
+ continue
21
+ name, id, size, modified = line.split("\t")
22
+ models.append({
23
+ "name": name,
24
+ "id": id,
25
+ "size": size,
26
+ "modified": modified
27
+ })
28
+ return models
29
+ except Exception as e:
30
+ return [{"error": str(e)}]
31
+
32
+
33
+ def pull_model(model_name: str) -> str:
34
+ """Pull an Ollama model"""
35
+ try:
36
+ result = subprocess.run(
37
+ ["ollama", "pull", model_name],
38
+ capture_output=True,
39
+ text=True,
40
+ check=True
41
+ )
42
+ return f"Pomyślnie pobrano model: {model_name}\n{result.stdout}"
43
+ except subprocess.CalledProcessError as e:
44
+ return f"Błąd podczas pobierania modelu: {e.stderr}"
45
+ except Exception as e:
46
+ return f"Nieoczekiwany błąd: {str(e)}"
47
+
48
+
49
+ def run_model(prompt: str, model_name: str = "llama3") -> str:
50
+ """Run an Ollama model with a prompt"""
51
+ try:
52
+ result = subprocess.run(
53
+ ["ollama", "run", model_name, prompt],
54
+ capture_output=True,
55
+ text=True,
56
+ check=True,
57
+ timeout=60 # 1 minute timeout
58
+ )
59
+ return result.stdout
60
+ except subprocess.CalledProcessError as e:
61
+ return f"Błąd podczas uruchamiania modelu:\n{e.stderr}"
62
+ except Exception as e:
63
+ return f"Nieoczekiwany błąd: {str(e)}"
64
+
65
+
66
+ def get_model_info(model_name: str) -> str:
67
+ """Get information about a specific Ollama model"""
68
+ try:
69
+ result = subprocess.run(
70
+ ["ollama", "show", model_name],
71
+ capture_output=True,
72
+ text=True,
73
+ check=True
74
+ )
75
+ return result.stdout
76
+ except Exception as e:
77
+ return f"Błąd podczas pobierania informacji o modelu: {str(e)}"
78
+
79
+
80
+ with gr.Blocks(title="Ollama Server Example", theme=gr.themes.Soft()) as demo:
81
+ gr.Markdown("# 🦙 Ollama Server Example\nProsty serwer Ollama na Hugging Face Spaces z interfejsem Gradio")
82
+
83
+ with gr.Tabs():
84
+ with gr.Tab("🔄 Uruchom Model"):
85
+ with gr.Row():
86
+ model_input = gr.Textbox(
87
+ label="Nazwa modelu (domyślnie: llama3)",
88
+ value="llama3",
89
+ interactive=True
90
+ )
91
+ prompt_input = gr.Textbox(
92
+ label="Prompt",
93
+ placeholder="Wprowadź tekst...",
94
+ lines=3
95
+ )
96
+
97
+ run_btn = gr.Button("🚀 Uruchom Model", variant="primary")
98
+ output_text = gr.Textbox(label="Wynik", lines=10)
99
+
100
+ run_btn.click(
101
+ fn=run_model,
102
+ inputs=[prompt_input, model_input],
103
+ outputs=output_text
104
+ )
105
+
106
+ with gr.Tab("📦 Pobierz Model"):
107
+ with gr.Row():
108
+ pull_model_input = gr.Textbox(
109
+ label="Nazwa modelu do pobrania (np. llama3, mistral)",
110
+ placeholder="llama3"
111
+ )
112
+
113
+ pull_btn = gr.Button("💾 Pobierz Model", variant="secondary")
114
+ pull_output = gr.Textbox(label="Status pobierania", lines=5)
115
+
116
+ pull_btn.click(
117
+ fn=pull_model,
118
+ inputs=pull_model_input,
119
+ outputs=pull_output
120
+ )
121
+
122
+ with gr.Tab("📊 Lista Modeli"):
123
+ refresh_btn = gr.Button("🔄 Odśwież Listę Modeli", variant="primary")
124
+ models_table = gr.JSON(label="Dostępne modele")
125
+
126
+ refresh_btn.click(
127
+ fn=list_models,
128
+ outputs=models_table
129
+ )
130
+
131
+ # Load models on startup
132
+ demo.load(
133
+ fn=list_models,
134
+ outputs=models_table
135
+ )
136
+
137
+ with gr.Tab("ℹ️ Informacje o Modelu"):
138
+ with gr.Row():
139
+ info_model_input = gr.Textbox(
140
+ label="Nazwa modelu",
141
+ value="llama3"
142
+ )
143
+
144
+ info_btn = gr.Button("📋 Pobierz Informacje", variant="secondary")
145
+ info_output = gr.Textbox(label="Informacje o modelu", lines=15)
146
+
147
+ info_btn.click(
148
+ fn=get_model_info,
149
+ inputs=info_model_input,
150
+ outputs=info_output
151
+ )
152
+
153
+
154
+ if __name__ == "__main__":
155
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio>=4.0.0
2
+ ollama