-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
79 lines (65 loc) · 2.34 KB
/
app.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import streamlit as st
import requests
import json
def get_ollama_models():
try:
response = requests.get('http://localhost:11434/api/tags')
if response.status_code == 200:
models = [model['name'] for model in response.json()['models']]
return models
return []
except:
return ['llama2', 'codellama', 'mistral'] # Default models if server not accessible
def generate_response(prompt, model):
url = "http://localhost:11434/api/generate"
data = {
"model": model,
"prompt": prompt,
"stream": False
}
try:
response = requests.post(url, json=data)
if response.status_code == 200:
return response.json()['response']
return "Error: Unable to get response from model"
except Exception as e:
return f"Error: {str(e)}"
def main():
st.title("🤖 SAWAS Prompt Enhancer")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Sidebar for model selection
with st.sidebar:
st.header("Settings")
models = get_ollama_models()
selected_model = st.selectbox("Select Model", models)
st.markdown("---")
st.markdown("### About")
st.markdown("This is a simple chat interface for Ollama models.")
st.markdown("Make sure Ollama is running locally!")
# Display chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Chat input
if prompt := st.chat_input("What would you like to ask?"):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Generate response
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
response = generate_response(prompt, selected_model)
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})
if __name__ == "__main__":
main()
#!/usr/bin/env python
"""
Author: Veera Babu Manyam
Organization: SAWAS
License: Apache License
"""
# Frontend application entry point