Npps commited on
Commit
5cc3f6e
·
verified ·
1 Parent(s): 9da2863
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from langchain_core.prompts import ChatPromptTemplate
4
+ from langchain_core.messages import HumanMessage, SystemMessage
5
+ from langchain_huggingface import HuggingFaceEndpoint
6
+ from langchain_core.output_parsers import StrOutputParser
7
+
8
+ # Ensure environment variables are loaded
9
+ langchain_key = os.getenv("LANGCHAIN_API_KEY")
10
+ HF_key = os.getenv("HUGGINGFACEHUB_TOKEN")
11
+
12
+ if langchain_key is None or HF_key is None:
13
+ raise ValueError("API keys are not set. Please set LANGCHAIN_API_KEY and HUGGINGFACEHUB_TOKEN.")
14
+
15
+ os.environ["LANGCHAIN_TRACING_V2"] = "true"
16
+ os.environ["LANGCHAIN_API_KEY"] = langchain_key
17
+ os.environ['HUGGINGFACEHUB_TOKEN'] = HF_key
18
+
19
+ # 1. Prompt Template
20
+ system_template = "Translate the following from into {language}:"
21
+ human_template = "{text}"
22
+
23
+ prompt_template = ChatPromptTemplate.from_messages([
24
+ SystemMessage(content=system_template),
25
+ HumanMessage(content=human_template)
26
+ ])
27
+
28
+ # 2. Create model
29
+ llm = HuggingFaceEndpoint(
30
+ repo_id="mistralai/Mistral-7B-Instruct-v0.3",
31
+ task="translation",
32
+ max_new_tokens=150,
33
+ do_sample=False,
34
+ token=HF_key
35
+ )
36
+
37
+ # 3. Create parser
38
+ parser = StrOutputParser()
39
+
40
+ # 4. Create chain
41
+ chain = prompt_template | llm | parser
42
+
43
+ def translate(text, language):
44
+ input_data = {
45
+ "text": text,
46
+ "language": language
47
+ }
48
+ # Use the .invoke() method to properly run the chain
49
+ result = chain.invoke(input_data)
50
+ return result
51
+
52
+ # Gradio interface
53
+ iface = gr.Interface(
54
+ fn=translate,
55
+ inputs=[
56
+ gr.Textbox(lines=2, placeholder="Enter text to translate"),
57
+ gr.Textbox(lines=1, placeholder="Enter target language")
58
+ ],
59
+ outputs="text",
60
+ title="LangChain Translation Service",
61
+ description="Translate text using LangChain and Hugging Face."
62
+ )
63
+
64
+ if __name__ == "__main__":
65
+ iface.launch()