rushankg commited on
Commit
29eabe2
·
verified ·
1 Parent(s): c091e7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -24
app.py CHANGED
@@ -1,22 +1,9 @@
1
- import streamlit as st
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
- import torch
4
  import os
5
- from huggingface_hub import login
6
-
7
- # Authenticate with Hugging Face using the environment variable
8
- huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
9
- login(token=huggingface_token)
10
-
11
- # Load the tokenizer and model from Hugging Face
12
- @st.cache_resource
13
- def load_model():
14
- model_name = "meta-llama/Meta-Llama-3.1-70B-Instruct"
15
- tokenizer = AutoTokenizer.from_pretrained(model_name)
16
- model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
17
- return tokenizer, model
18
 
19
- tokenizer, model = load_model()
 
20
 
21
  # Supported languages
22
  languages = ['English', 'French', 'Spanish', 'Hindi', 'Punjabi']
@@ -45,14 +32,22 @@ def main():
45
  st.success("Translation:")
46
  st.write(translation)
47
 
48
- # Function to translate text using the LLaMA model
49
  def translate_text(text, input_language, output_language):
50
- prompt = f"Translate the following from {input_language} to {output_language}:\n\n{text}"
51
- inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
52
- with torch.no_grad():
53
- outputs = model.generate(**inputs, max_new_tokens=200)
54
- translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
55
- return translation
 
 
 
 
 
 
 
 
56
 
57
  if __name__ == "__main__":
58
  main()
 
 
 
 
1
  import os
2
+ import openai
3
+ import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ # Set up OpenAI API key
6
+ openai.api_key = os.getenv("OPENAI_KEY")
7
 
8
  # Supported languages
9
  languages = ['English', 'French', 'Spanish', 'Hindi', 'Punjabi']
 
32
  st.success("Translation:")
33
  st.write(translation)
34
 
35
+ Function to translate text using GPT-3.5
36
  def translate_text(text, input_language, output_language):
37
+ try:
38
+ response = openai.ChatCompletion.create(
39
+ model="gpt-3.5-turbo",
40
+ messages=[
41
+ {"role": "system", "content": f"You translate text from {input_language} to {output_language}"},
42
+ {"role": "user", "content": text}
43
+ ],
44
+ max_tokens=5000
45
+ )
46
+ translation = response.choices[0].message['content'].strip()
47
+ return translation
48
+
49
+ except Exception as e:
50
+ return f"Error: {str(e)}"
51
 
52
  if __name__ == "__main__":
53
  main()