Dhahlan2000 commited on
Commit
5b1ccca
·
verified ·
1 Parent(s): 8497f03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -2,6 +2,11 @@ import gradio as gr
2
  from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
3
  from aksharamukha import transliterate
4
  import torch
 
 
 
 
 
5
 
6
  # Set up device
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -46,10 +51,17 @@ def transliterate_to_sinhala(text):
46
  # Placeholder for conversation model loading and pipeline setup
47
  # pipe1 = pipeline("text-generation", model="microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
48
 
49
- interface = gr.Interface.load("huggingface/microsoft/Phi-3-mini-4k-instruct")
 
 
 
50
 
51
- def conversation_predict(text):
52
- return interface([text])[0]
 
 
 
 
53
 
54
  def ai_predicted(user_input):
55
  if user_input.lower() == 'exit':
@@ -58,8 +70,10 @@ def ai_predicted(user_input):
58
  user_input = translate_Singlish_to_sinhala(user_input)
59
  user_input = transliterate_to_sinhala(user_input)
60
  user_input = translate_sinhala_to_english(user_input)
61
- # ai_response = pipe1([{"role": "user", "content": user_input}])
62
- ai_response = conversation_predict(user_input)
 
 
63
  ai_response_lines = ai_response.split("</s>")
64
 
65
  response = translate_english_to_sinhala(ai_response_lines[-1])
@@ -85,8 +99,6 @@ def respond(
85
 
86
  messages.append({"role": "user", "content": message})
87
 
88
-
89
-
90
  response = ai_predicted(message)
91
 
92
  yield response
 
2
  from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
3
  from aksharamukha import transliterate
4
  import torch
5
+ from dotenv import load_dotenv
6
+ import os
7
+
8
+ load_dotenv()
9
+ access_token = os.getenv('ACCESS_TOKEN')
10
 
11
  # Set up device
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
51
  # Placeholder for conversation model loading and pipeline setup
52
  # pipe1 = pipeline("text-generation", model="microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
53
 
54
+ # interface = gr.Interface.load("huggingface/microsoft/Phi-3-mini-4k-instruct")
55
+
56
+ API_URL = "https://api-inference.huggingface.co/models/microsoft/Phi-3-mini-4k-instruct"
57
+ headers = {"Authorization": f"Bearer {access_token}"}
58
 
59
+ def query(payload):
60
+ response = requests.post(API_URL, headers=headers, json=payload)
61
+ return response.json()
62
+
63
+ # def conversation_predict(text):
64
+ # return interface([text])[0]
65
 
66
  def ai_predicted(user_input):
67
  if user_input.lower() == 'exit':
 
70
  user_input = translate_Singlish_to_sinhala(user_input)
71
  user_input = transliterate_to_sinhala(user_input)
72
  user_input = translate_sinhala_to_english(user_input)
73
+ ai_response = query({
74
+ "inputs": user_input,
75
+ })
76
+ # ai_response = conversation_predict(user_input)
77
  ai_response_lines = ai_response.split("</s>")
78
 
79
  response = translate_english_to_sinhala(ai_response_lines[-1])
 
99
 
100
  messages.append({"role": "user", "content": message})
101
 
 
 
102
  response = ai_predicted(message)
103
 
104
  yield response