-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
98 lines (76 loc) · 2.88 KB
/
run.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from flask import Flask, render_template, request, Response
import google.generativeai as genai
app = Flask(__name__)
# Fetch the API key from the environment variable
GOOGLE_API_KEY = 'AIzaSyBi3fQCz0JRVvgpH0BOSOZS702nml5zUbE'
if not GOOGLE_API_KEY:
raise ValueError("No API key found. Please set the GOOGLE_API_KEY environment variable.")
# Initialize the API with the fetched key
genai.configure(api_key=GOOGLE_API_KEY)
try:
model = genai.GenerativeModel('tunedModels/sum-v5tolb1ijmey')
except:
print("Permission denied for fine-tuned model. Falling back to default model.")
# Load the fine-tuned model
# model= genai.GenerativeModel('tunedModels/datacaption-cmb7ktrqhnum')
def parse_response(text):
# Initialize the output and states
html_output = ""
in_list = False
list_level = 0
# Split the text by lines
lines = text.split('\n')
for line in lines:
# Handle bold headers
if line.startswith("**") and line.endswith("**"):
if in_list:
html_output += "</ul>\n"
in_list = False
html_output += f"<h3>{line.strip('**').strip()}</h3>\n"
# Handle bullet points
elif line.startswith("*") and not (line.startswith("**") and line.endswith("**")):
# Check if we're already in a list
if not in_list:
html_output += "<ul>\n"
in_list = True
# Check for nesting (if there are nested lists)
if line.count('*') > list_level:
html_output += "<ul>\n"
list_level = line.count('*')
elif line.count('*') < list_level:
html_output += "</ul>\n"
list_level = line.count('*')
html_output += f" <li>{line.strip('*').strip()}</li>\n"
# Handle paragraphs and ensure lists are closed
else:
if in_list:
html_output += "</ul>\n"
in_list = False
html_output += f"<p>{line.strip()}</p>\n"
# Close any remaining open lists
if in_list:
html_output += "</ul>\n"
return html_output
def chat_with_bot(prompt):
# Create a chat session
chat = model.start_chat(history=[])
# Send the message and stream the response
response = chat.send_message(prompt, stream=True)
response_text = ""
for chunk in response:
if chunk.text:
response_text += chunk.text + ' '
# Parse the accumulated response into HTML
parsed_html = parse_response(response_text)
# Yield the parsed HTML content
yield parsed_html
@app.route('/')
def index():
return render_template('index.html')
@app.route('/chat', methods=['POST'])
def chat():
data = request.get_json()
prompt = data.get('prompt')
return Response(chat_with_bot(prompt), content_type='text/plain')
if __name__ == "__main__":
app.run(debug=True)