如何将后端AI python编码与html前端连接?

时间:2019-05-27 09:33:50

标签: html css python-3.x artificial-intelligence

我通过人工智能python程序开发了股票市场预测。 但是需要在internet.so中发布为网页,所以我使用CSS和JavaScript创建了html页面。但是我不知道如何将html页面与我的AI程序连接起来。

我的html代码:

body{
	background-color: #E9F5F7;
	font-family: arial, sans-serif;
	margin: 0 auto;
}
.header{
	width: 100%;
	margin: 0 auto;
	height: 80px;
	background-color: #301E92;
	position: fixed;
	color: 000203;
	padding-top: 20px;
	font-size: 18px;
	font-size: 18px;
	font-weight: bold;
}
.header img {
  float: left;
  width: 100px;
  height: 80px;
  background: #555;
}
.header h1 {
  position: sticky;
  text-align: center;
  top: 18px;
  left: 10px;
}
h2{
	color: #F8CD82;
}
.hero-image {
  background-image: url("/trail/img/back.png");
  background-color: 000203;
  height: 300px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;
}
h2{
	font-family: Pacifico;
}
.autocomplete {
  position: relative;
  display: inline-block;
}

input {
  border: 1px solid transparent;
  background-color: #f1f1f1;
  padding: 10px;
  font-size: 16px;
}

input[type=text] {
  background-color: #f1f1f1;
  width: 100%;
}

input[type=submit] {
  background-color: DodgerBlue;
  color: #fff;
  cursor: pointer;
  width:100px;
}

.autocomplete-items {
  position: absolute;
  border: 1px solid #d4d4d4;
  border-bottom: none;
  border-top: none;
  z-index: 99;
  /*position the autocomplete items to be the same width as the container:*/
  top: 100%;
  left: 0;
  right: 0;
}

.autocomplete-items div {
  padding: 10px;
  cursor: pointer;
  background-color: #fff; 
  border-bottom: 1px solid #d4d4d4; 
}

/*when hovering an item:*/
.autocomplete-items div:hover {
  background-color: #e9e9e9; 
}

/*when navigating through the items using the arrow keys:*/
.autocomplete-active {
  background-color: DodgerBlue !important; 
  color: #ffffff; 
}
<!DOCTYPE html>
<html>
<head>
<title>Stock Market predictor</title>
<link rel="stylesheet" href="style1.css">
</head>
<body>
<div class="header">
	<img src="img/logo.png" alt="logo" />
	<h1>Stock Market predictor<h1>	
	<h3>About as</h3>
	<h4>Contact as</h4>
<div>
<div class="hero-image">
  <div class="hero-text">
    <h2>Welcome to Stock Market predictor</h2>
	</div>
	<form autocomplete="off" action="/action_page.php">
  <div class="autocomplete" style="width:300px;">
    <input id="myInput" type="text" name="symbols" placeholder="symbols">
  </div>
  <input type="submit">
</form>
</div>
</body>
</html>

但是我不知道如何连接AI代码:

import json as j
import pandas as pd
import numpy as np
import simplejson as json
import csv
import sys
import os
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction.text import TfidfTransformer


# --- read and transform json file
json_data = None
with open('D:/materials/AI/science day/yelp_academic_dataset_review.json') as data_file:
    lines = data_file.readlines()
    joined_lines = "[" + ",".join(lines) + "]"

    json_data = j.loads(joined_lines)


data = pd.DataFrame(json_data)
print(data.head())
LogisticRegression(solver='lbfgs')


# --- prepare the data

data = data[data.stars != 3]
data['sentiment'] = data['stars'] >= 4


# --- build the model  

X_train, X_test, y_train, y_test = train_test_split(data, data.sentiment, test_size=0.2)

# -
count = CountVectorizer()
temp = count.fit_transform(X_train.text)

tdif = TfidfTransformer()
temp2 = tdif.fit_transform(temp)

text_regression = LogisticRegression()
model = text_regression.fit(temp2, y_train)

prediction_data = tdif.transform(count.transform(X_test.text))

predicted = model.predict(prediction_data)

# instead of doing all this steps above one could also use Pipeline
# this is a more compact way of writing the code above...
# it also has the benefit that there is no need to perform the transformations on the test data
#
#
from sklearn.pipeline import Pipeline
text_regression = Pipeline([('count', CountVectorizer()), ('tfidf', TfidfTransformer()),('reg', LogisticRegression())])

model = text_regression.fit(X_train.text, y_train)
predicted = model.predict(X_test.text)


# --- make predictions


print(np.mean(predicted == y_test))

# --- have some fun with the model

with open('news.csv', newline='') as myFile:  
    reader = csv.reader(myFile)
    for row in reader:
        n=row

value = model.predict(row)
print(value)
if(value[0]==True):
    s="positive"

else:
    s="negative"

os.system("python interface3.py "+str(s)+" "+str(n))

能否请您解释一下如何将此前端和后端连接?

0 个答案:

没有答案