Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, render_template, request, jsonify, send_file
|
| 2 |
import ollama
|
| 3 |
import json
|
| 4 |
import re
|
|
@@ -10,7 +10,7 @@ import urllib3
|
|
| 10 |
import pandas as pd
|
| 11 |
import io
|
| 12 |
import ast
|
| 13 |
-
|
| 14 |
|
| 15 |
app = Flask(__name__)
|
| 16 |
|
|
@@ -62,18 +62,25 @@ No details or explainations are required, just the results in the required **JSO
|
|
| 62 |
"""
|
| 63 |
|
| 64 |
|
| 65 |
-
def ask_ollama(user_message, model='
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
print(f"AI REPLY json:\n{ai_reply}")
|
| 78 |
|
| 79 |
# Process the response to ensure we return valid JSON
|
|
@@ -81,7 +88,7 @@ def ask_ollama(user_message, model='gemma3:1b', system_prompt=search_prompt):
|
|
| 81 |
# First, try to parse it directly in case it's already valid JSON
|
| 82 |
print(f"AI REPLY:\n{ai_reply}")
|
| 83 |
return ast.literal_eval(ai_reply.replace('json\n', '').replace('```', ''))
|
| 84 |
-
except:
|
| 85 |
print(f"ERROR:\n{e}")
|
| 86 |
# If it's not valid JSON, try to extract JSON from the text
|
| 87 |
return {
|
|
@@ -174,13 +181,9 @@ def analyze_pdf_novelty(patent_background, url, data_type="pdf"):
|
|
| 174 |
except Exception as e:
|
| 175 |
return {"error": f"Error: {str(e)}"}
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
@app.get("/")
|
| 182 |
-
def timeline():
|
| 183 |
-
return stream_template("index.html")
|
| 184 |
|
| 185 |
@app.route('/chat', methods=['POST'])
|
| 186 |
def chat():
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify, send_file
|
| 2 |
import ollama
|
| 3 |
import json
|
| 4 |
import re
|
|
|
|
| 10 |
import pandas as pd
|
| 11 |
import io
|
| 12 |
import ast
|
| 13 |
+
from groq import Groq
|
| 14 |
|
| 15 |
app = Flask(__name__)
|
| 16 |
|
|
|
|
| 62 |
"""
|
| 63 |
|
| 64 |
|
| 65 |
+
def ask_ollama(user_message, model='llama-3-8b', system_prompt=search_prompt):
|
| 66 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 67 |
+
|
| 68 |
+
response = client.chat.completions.create(
|
| 69 |
+
model=model,
|
| 70 |
+
messages=[
|
| 71 |
+
{
|
| 72 |
+
"role": "system",
|
| 73 |
+
"content": system_prompt
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"role": "user",
|
| 77 |
+
"content": user_message
|
| 78 |
+
}
|
| 79 |
+
],
|
| 80 |
+
stream=False,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
ai_reply = response.choices[0].message.content
|
| 84 |
print(f"AI REPLY json:\n{ai_reply}")
|
| 85 |
|
| 86 |
# Process the response to ensure we return valid JSON
|
|
|
|
| 88 |
# First, try to parse it directly in case it's already valid JSON
|
| 89 |
print(f"AI REPLY:\n{ai_reply}")
|
| 90 |
return ast.literal_eval(ai_reply.replace('json\n', '').replace('```', ''))
|
| 91 |
+
except Exception as e:
|
| 92 |
print(f"ERROR:\n{e}")
|
| 93 |
# If it's not valid JSON, try to extract JSON from the text
|
| 94 |
return {
|
|
|
|
| 181 |
except Exception as e:
|
| 182 |
return {"error": f"Error: {str(e)}"}
|
| 183 |
|
| 184 |
+
@app.route('/')
|
| 185 |
+
def home():
|
| 186 |
+
return render_template('index.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
@app.route('/chat', methods=['POST'])
|
| 189 |
def chat():
|