Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
# -----------------------------------------------------
|
6 |
+
# Streamlit Page Configuration
|
7 |
+
# -----------------------------------------------------
|
8 |
+
st.set_page_config(page_title="Ollama API Tester", layout="wide")
|
9 |
+
st.title("π§ͺ Ollama API Model Tester")
|
10 |
+
st.subheader("Test Ollama via API in Hugging Face Spaces.")
|
11 |
+
|
12 |
+
# -----------------------------------------------------
|
13 |
+
# API Key and URL (Replace with Your Ollama Server)
|
14 |
+
# -----------------------------------------------------
|
15 |
+
OLLAMA_API_URL = "https://your-ollama-server.com/api/chat" # Change this to your server
|
16 |
+
|
17 |
+
# -----------------------------------------------------
|
18 |
+
# User Input: Prompt
|
19 |
+
# -----------------------------------------------------
|
20 |
+
user_prompt = st.text_area("βοΈ Enter your prompt:", "Explain quantum computing in simple terms.")
|
21 |
+
|
22 |
+
# -----------------------------------------------------
|
23 |
+
# Button to Generate Response
|
24 |
+
# -----------------------------------------------------
|
25 |
+
if st.button("π Generate Response"):
|
26 |
+
with st.spinner("Fetching Ollama response..."):
|
27 |
+
try:
|
28 |
+
# Prepare API payload
|
29 |
+
payload = {
|
30 |
+
"model": "mistral", # Change model name if needed
|
31 |
+
"messages": [{"role": "user", "content": user_prompt}]
|
32 |
+
}
|
33 |
+
|
34 |
+
# Send request to Ollama API
|
35 |
+
response = requests.post(OLLAMA_API_URL, json=payload)
|
36 |
+
|
37 |
+
# Process Response
|
38 |
+
if response.status_code == 200:
|
39 |
+
response_text = response.json()["message"]["content"]
|
40 |
+
st.markdown("### π€ Ollama API Response:")
|
41 |
+
st.write(f"π {response_text}")
|
42 |
+
else:
|
43 |
+
st.error(f"β Ollama API Error: {response.json()}")
|
44 |
+
|
45 |
+
except Exception as e:
|
46 |
+
st.error(f"β Ollama Request Failed: {e}")
|