Spaces:
Configuration error

yonkasoft commited on
Commit
2ccc687
1 Parent(s): 719954c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import pymongo
3
+ import requests
4
+ import json
5
+ from transformers import MT5ForConditionalGeneration, MT5Tokenizer
6
+ import torch
7
+ from torch.nn.functional import softmax
8
+
9
+ app = Flask(__name__)
10
+
11
+ # MongoDB bağlantısı
12
+ mongo_client = pymongo.MongoClient('mongodb://localhost:27017/')
13
+ db = mongo_client['EgitimDatabase']
14
+ collection = db['test']
15
+
16
+ # Model ve tokenizer'ı yükleme
17
+ model_name = "alan-turing-institute/mt5-large-finetuned-mnli-xtreme-xnli"
18
+ tokenizer = MT5Tokenizer.from_pretrained(model_name)
19
+ model = MT5ForConditionalGeneration.from_pretrained(model_name)
20
+ model.eval()
21
+
22
+ def process_nli(premise: str, hypothesis: str):
23
+ """NLI formatına uygun hale getirir"""
24
+ return "".join(['xnli: premise: ', premise, ' hypothesis: ', hypothesis])
25
+
26
+ def generate_text(title, keywords, subheadings):
27
+ # Prompt oluşturma
28
+ prompt = f"Başlık: {title}\nAnahtar Kelimeler: {keywords}\nAlt Başlıklar: {subheadings}\n\nBu bilgilerle bir metin oluşturun:"
29
+ inputs = tokenizer(prompt, return_tensors="pt")
30
+
31
+ # Modelden çıktı alma
32
+ outputs = model.generate(**inputs, output_scores=True, return_dict_in_generate=True, num_beams=5)
33
+ generated_text = tokenizer.decode(outputs.sequences[0], skip_special_tokens=True)
34
+
35
+ return generated_text
36
+
37
+ @app.route('/predict', methods=['POST'])
38
+ def predict():
39
+ data = request.json
40
+ title = data.get('title')
41
+ keywords = data.get('keywords')
42
+ subheadings = data.get('subheadings')
43
+
44
+ # MongoDB'den ilgili verileri çekme
45
+ query = {
46
+ 'title': title,
47
+ 'keywords': {'$in': keywords.split(',')},
48
+ 'subheadings': {'$in': subheadings.split(',')}
49
+ }
50
+ documents = list(collection.find(query))
51
+
52
+ if not documents:
53
+ return jsonify({'error': 'No documents found matching the query'}), 404
54
+
55
+ # Verilerle metin oluşturma
56
+ generated_texts = []
57
+ for doc in documents:
58
+ generated_text = generate_text(doc['title'], doc['keywords'], doc['subheadings'])
59
+ generated_texts.append(generated_text)
60
+
61
+ # Sonuçları döndürme
62
+ response = {
63
+ 'title': title,
64
+ 'keywords': keywords,
65
+ 'subheadings': subheadings,
66
+ 'generated_texts': generated_texts
67
+ }
68
+
69
+ return jsonify(response)
70
+
71
+ if __name__ == "__main__":
72
+ app.run(host='0.0.0.0', port=8080)