Skip to content

Instantly share code, notes, and snippets.

@tiru9
Created February 1, 2025 18:45
Show Gist options
  • Save tiru9/22fe1a37c4235e7a697fc7fcab041e6c to your computer and use it in GitHub Desktop.
Save tiru9/22fe1a37c4235e7a697fc7fcab041e6c to your computer and use it in GitHub Desktop.
from flask import Flask, render_template, request, jsonify
import requests
app = Flask(__name__)
OLLAMA_URL = "http://localhost:11434/api/generate"
@app.route("/")
def home():
return render_template("index.html")
@app.route("/ask", methods=["POST"])
def ask():
user_input = request.json.get("prompt", "")
if not user_input:
return jsonify({"response": "No prompt provided!"})
payload = {
"model": "deepseek-r1:1.5b",
"prompt": user_input,
"stream": False # Ensures we get a complete response at once
}
response = requests.post(OLLAMA_URL, json=payload)
try:
data = response.json()
print("🔍 Ollama API Response:", data) # Debugging: Print the response
# Check if "response" key exists
if "response" not in data:
return jsonify({"response": "Error: Unexpected API response format", "debug": data})
return jsonify({"response": data["response"]})
except Exception as e:
return jsonify({"response": f"Error: {str(e)}"})
if __name__ == "__main__":
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment