Spaces:
Build error
Build error
Commit
·
5af835e
1
Parent(s):
e17086d
Delete gradio_app.py
Browse files- gradio_app.py +0 -75
gradio_app.py
DELETED
@@ -1,75 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import time
|
3 |
-
import csv
|
4 |
-
import datetime
|
5 |
-
import gradio
|
6 |
-
import schedule
|
7 |
-
from gradio import utils
|
8 |
-
import huggingface_hub
|
9 |
-
from pathlib import Path
|
10 |
-
from src.utils.utilities import Utility
|
11 |
-
from transformers import RobertaTokenizer, T5ForConditionalGeneration
|
12 |
-
|
13 |
-
dataset_dir = "logs"
|
14 |
-
headers = ["input", "output", "timestamp", "elapsed"]
|
15 |
-
repo = huggingface_hub.Repository(
|
16 |
-
local_dir=dataset_dir,
|
17 |
-
clone_from="https://huggingface.co/spaces/priyasaravana/CodeSummarization",
|
18 |
-
token=hf_token,
|
19 |
-
)
|
20 |
-
repo.git_pull(lfs=True)
|
21 |
-
|
22 |
-
def log_record(vals):
|
23 |
-
log_file = Path(dataset_dir) / "data.csv"
|
24 |
-
is_new = not Path(log_file).exists()
|
25 |
-
with open(log_file, "a", newline="", encoding="utf-8") as csvfile:
|
26 |
-
writer = csv.writer(csvfile)
|
27 |
-
if is_new:
|
28 |
-
writer.writerow(utils.sanitize_list_for_csv(headers))
|
29 |
-
writer.writerow(utils.sanitize_list_for_csv(vals))
|
30 |
-
schedule.run_pending()
|
31 |
-
print(f"Last Sync: {job.last_run}")
|
32 |
-
|
33 |
-
def evaluate(sentence):
|
34 |
-
tokenizer = RobertaTokenizer.from_pretrained('Salesforce/codet5-base')
|
35 |
-
model = T5ForConditionalGeneration.from_pretrained('Salesforce/codet5-base-multi-sum')
|
36 |
-
|
37 |
-
# Prepare the input text
|
38 |
-
input_text = code_snippet.strip()
|
39 |
-
input_ids = tokenizer.encode(input_text, return_tensors='pt')
|
40 |
-
# Generate a summary
|
41 |
-
generated_ids = model.generate(input_ids, max_length=20)
|
42 |
-
summary = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
43 |
-
|
44 |
-
return summary
|
45 |
-
|
46 |
-
def predict(sentence):
|
47 |
-
timestamp = datetime.datetime.now().isoformat()
|
48 |
-
start_time = time.time()
|
49 |
-
predictions = evaluate([sentence])
|
50 |
-
elapsed_time = time.time() - start_time
|
51 |
-
output = predictions
|
52 |
-
print(f"Sentence: {sentence} \nPrediction: {predictions}")
|
53 |
-
log_record([sentence, output, timestamp, str(elapsed_time)])
|
54 |
-
|
55 |
-
return output
|
56 |
-
|
57 |
-
def sync_logs():
|
58 |
-
print(f"Repo Clean: {repo.is_repo_clean()}")
|
59 |
-
if not repo.is_repo_clean():
|
60 |
-
repo.git_add()
|
61 |
-
repo.git_commit()
|
62 |
-
repo.git_pull(lfs=True)
|
63 |
-
result = repo.git_push()
|
64 |
-
# result = repo.push_to_hub()
|
65 |
-
print(result)
|
66 |
-
|
67 |
-
job = schedule.every(5).minutes.do(sync_logs)
|
68 |
-
print("Scheduler engaged")
|
69 |
-
|
70 |
-
gradio.Interface(
|
71 |
-
fn=predict,
|
72 |
-
inputs="text",
|
73 |
-
outputs="text",
|
74 |
-
allow_flagging='never'
|
75 |
-
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|